LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-02-2002, 02:15 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
question about C 'for' statement ...


Hi. How does an array get stored?

say
char array[ ] = "linux";

array[0] is 'l'
array[1] is 'i'
etc,

but is there a '\0' in the 5th cell or EOF
Question arises cause I got a segmentation fault in the following>

char array [ ] = "linux";

for( i = 0; i != '\0'; i++)
printf("array[%d] contains %c\n", i, array[i]);

the output just kept printing a long list of array contents to stdout and ended saying segmentation fault

I probably am not checking for EOF correctly in the 'for' loop.
Say char array[10] was to be filled in by the program asking user for input?
I would'nt want to print the contents of all the cells he or she didnt fill ( say if user only types name 'Bill' into the array) How ... using the 'for' loop would I check to stop 'i' from further augmenting at end of the array?
I thought it was 'i != '\0' ' or i != EOF

Can someone help?


thanks
 
Old 08-02-2002, 05:03 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Code:
#include <stdio.h>

int main() {
	char tab[]="linux";
	int i=0;
	for(i=0;tab[i]!='\0';i++)
		printf("tab[%d]: %c\n",i,tab[i]);
	return 0;
}
And it's output:
Code:
tab[0]: l
tab[1]: i
tab[2]: n
tab[3]: u
tab[4]: x
So, where's the problem?
 
Old 08-02-2002, 06:43 AM   #3
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
Hi Mara, he is trying to compare i != '\0'. Thats where it fails.
--Sarin
 
Old 08-02-2002, 10:41 AM   #4
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thank you Mara. I was doing as sarin said. 'i' wouldn't equal '\0'. Forgive me?
Im new to C. But I love it!

thanks for your help.
 
Old 08-02-2002, 05:56 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I wrote that short program and was wondering why purpleburple has a problem. I haven't realized this 'i != '\0'. Don't worry, purpleburple, such mistakes are very common...
 
Old 08-14-2002, 03:05 AM   #6
guest72485
LQ Newbie
 
Registered: Jun 2002
Posts: 8

Rep: Reputation: 0
Quote:
Originally posted by Mara
Code:
               char tab[]="linux";
how does a null terminating character gets introduced?

what would happen if i had the following declaration

char tab[5]="linux";

even in this case wil a null will be introduced? if yes then does that mean the size if incremented. if no then will the following code segment work?

for(int i=0;tab[i] != '\0';i++)
printf(" [%d] = [%c]",i,tab[i]);
 
Old 08-14-2002, 03:54 AM   #7
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
char tab[] = "linux";
automatically introduces the '\0' since you did not initially specify the number of characters you wanted in the array.

but if you explicitly specify
char tab[5];
then you will only be allocated an array of 5 char sized "spaces". if you don't explicitly specify the '\0', then it will produce a segmentation fault. what happens is most likely, your program will try to insert the '\0' and end up writing over another spot in memory that may be in use(or not, depending). since this is quite dangerous, i think the program will be halted by the OS in the case of linux.
 
Old 08-14-2002, 04:04 AM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally posted by guest72485






char tab[5]="linux";
Dangerous. '\0' will not be introducted, so using such a string will cause trouble.
 
Old 08-25-2002, 07:12 AM   #9
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Rep: Reputation: 0
hey y don't u try array[i]!='\0' its not with i which u r comparing its with the value with which u r checking the condition ,so try giving this in condition it will work
 
  


Reply



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
DEBUG statement question djgerbavore Programming 3 01-26-2005 09:37 AM
the FOR statement (C) sk84blood813 Programming 3 12-09-2004 08:25 AM
If Statement junjem0702 Programming 4 08-23-2004 09:49 AM
for statement c code question dragoon linux Programming 3 12-31-2003 11:17 AM
bash if statement question xscousr Programming 3 09-02-2003 11:58 AM

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

All times are GMT -5. The time now is 04:34 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