LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2002, 12:24 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
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!

Last edited by purpleburple; 08-06-2002 at 12:26 AM.
 
Old 08-06-2002, 12:30 AM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
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.

Last edited by neo77777; 08-06-2002 at 12:32 AM.
 
Old 08-06-2002, 12:38 AM   #3
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
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++)
 
Old 08-06-2002, 12:43 AM   #4
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
And also, while loo[p is endless

Last edited by neo77777; 08-06-2002 at 12:44 AM.
 
Old 08-06-2002, 12:47 AM   #5
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
So, get rid of while statement and enclose printf in for loop
 
Old 08-06-2002, 01:13 AM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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.
 
Old 08-06-2002, 03:00 AM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 08-06-2002, 10:35 AM   #8
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
And always check for array boundaries, it is a good programming habit - saves a lot of headache later. Code safely.
 
Old 08-06-2002, 03:46 PM   #9
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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.
 
Old 08-06-2002, 04:07 PM   #10
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Before you implant a chip to your brain precompile at least
whoami
 
Old 08-06-2002, 08:45 PM   #11
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
yeah I've been wanting to know who I was for years! Maybe Linux can finally tell me!!
 
Old 08-06-2002, 09:39 PM   #12
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
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.
 
Old 08-06-2002, 10:32 PM   #13
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
lol
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM
No loop# SeaSharp Linux - Newbie 3 09-06-2005 08:14 PM
WHILE LOOP in 'C' ']['HeBroken Programming 4 10-29-2004 01:42 AM
while-loop Thomas23 Programming 4 05-24-2004 03:35 PM
for loop klfreese Programming 16 08-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration