ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. Im new to C so bare with me. I know the following contains alot of 'never do this's' but i am doing it purely for demonstration reasons to learn as I am new and am expecting user (myself) to know the correct input format. The following loops endlessly when printed to stdout and I can't figure out why. Can someone explain to me why?
#include <stdio.h>
int main(void)
{
char array[10];
int i = 0;
puts("Enter your first name");
scanf("%s", &array);
/* augment i to match number of last cell containing a character */
for(i=0; array[i] != '\0'; i++)
/* print cells to stdout backwards */
while(array[i] >= 0){
printf("array[%d] contains %c\n", i, array[i]);
}
return 0;
}
Also was wondering a better way of performing the 'for' loop that simply augments i
thanks
C is fun!
Last edited by purpleburple; 08-06-2002 at 12:26 AM.
There is no stop condition on int i, you are trying to compare integer with a character '\0', I see you are looping thru array so make
for (i=0; i <10 ; i++)
P.S. ignore it for now, you just edited and I was replying ti the original.
Ok you are not checking for array boundaries, so congrats you are a little bit away from BUFFER OVERFLOW you are accessing the memory beyond array limits, so make it
for (i=0; array[i] != '\0' && i < 10; i++)
i did the following before reading your posts and now it works great>
#include <stdio.h>
int main(void)
{
char array[20];
int i = 0;
puts("Enter a character string under 20 characters");
scanf("%s", &array);
/* augment i to equal last occupied array cell */
while(array[i] != '\0'){
i++;
}
/* print to stdout backwards */
while( i >= 0 ){
printf("array[%d] contains %c\n", i, array[i] );
i = i - 1;
}
return 0;
}
thanks for your help
Is the above getting better to proper form?
PS - I don't know anything about BUFFER OVERFLOW yet so I can't really perform any checks
Last edited by purpleburple; 08-06-2002 at 01:15 AM.
Test your program, enter a string longer than 20 characters and look what happens. You should not leave it this way. Make the buffer (chat array[20]) bigger, let's say 512.
thanks but I am new and know nothing of 'array boundaries' and Buffer Overflow' yet. Im just gonna concentrate on basics for now. Im in no rush.
I've got >
Ivor Horton's 'Beginning C'
Sam's C in 24 hours
A bigger C in 24 days blue book
And tons of downloaded C PDF's and html tutorials
Im gonna try and slowly absorb all this info ... ]
then i want to move on to python and assembly ...... and then im gonna get a chip implanted in my brain and see if I can program it using my own built in memory and some electrical brain signals , and I'll most likely run Linux on it ...definetly not Windows ........... lol ....
thanx again ..
Last edited by purpleburple; 08-06-2002 at 03:53 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.