LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with the following C loop ..... (https://www.linuxquestions.org/questions/programming-9/help-with-the-following-c-loop-27292/)

purpleburple 08-06-2002 12:24 AM

help with the following C loop .....
 
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!

neo77777 08-06-2002 12:30 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.

neo77777 08-06-2002 12:38 AM

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++)

neo77777 08-06-2002 12:43 AM

And also, while loo[p is endless

neo77777 08-06-2002 12:47 AM

So, get rid of while statement and enclose printf in for loop

purpleburple 08-06-2002 01:13 AM

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 :(

Mara 08-06-2002 03:00 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.

neo77777 08-06-2002 10:35 AM

And always check for array boundaries, it is a good programming habit - saves a lot of headache later. Code safely.

purpleburple 08-06-2002 03:46 PM

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 ..:)

neo77777 08-06-2002 04:07 PM

Before you implant a chip to your brain precompile at least
whoami

purpleburple 08-06-2002 08:45 PM

yeah I've been wanting to know who I was for years! Maybe Linux can finally tell me!! :) :)

neo77777 08-06-2002 09:39 PM

And if during the surgery you want some psychiatrist's help fire up emacs press Meta-x and type doctor
Note: Meta - is usually Alt key.

purpleburple 08-06-2002 10:32 PM

lol


All times are GMT -5. The time now is 10:01 AM.