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! |
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++) |
And also, while loo[p is endless
|
So, get rid of while statement and enclose printf in for loop
|
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 :( |
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.
|
And always check for array boundaries, it is a good programming habit - saves a lot of headache later. Code safely.
|
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 ..:) |
Before you implant a chip to your brain precompile at least
whoami |
yeah I've been wanting to know who I was for years! Maybe Linux can finally tell me!! :) :)
|
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. |
lol
|
All times are GMT -5. The time now is 10:01 AM. |