LinuxQuestions.org
Visit Jeremy's Blog.
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 10-30-2006, 11:16 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Cannot assign NULL to array?


Hello thanks for reading my post.

I have a strange issue with assigning NULL to a string array in C.

I have created a structure data type with a 100 element array and then later in the program creating memeory location using malloc and then assigning NULL later. Hopefull the below code will make my bableing make sense to someone.

Code:
 
#define CSIZE 100

typedef struct { 
                 int options; 
                 char sarray[CSIZE]; 
               } SDATA;  
 
int main ()
{ 
         SDATA *temp; 

        temp = malloc(sizeof(SDATA)); 

       temp->options = 10; 
       temp->sarray = NULL; /* should point the first pointer of the array to NULL */ 
return 0; 
}
Note that this is a very simplistic example and I have some stuff missing but this gives a good idea of what im doing. Gcc complains that incompatible types in assignment.
 
Old 10-30-2006, 11:28 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by exvor
I have a strange issue with assigning NULL to a string array in C.
But that's not what you declared in your program. The declaration is:
Code:
typedef struct { 
                 int options; 
                 char sarray[CSIZE]; 
               } SDATA;
sarray is an array of characters. Specifically, it's an array of 100 characters (which is a string) - not an array of strings.

To declare an array of 100 strings, change the declaration to something like:
Code:
typedef struct { 
                 int options; 
                 char *sarray[CSIZE]; 
               } SDATA;
That will create an array of 100 character pointer elements (strings). You'll need to assign NULL to each of them for safety and manually allocate space for them later. For instance:
Code:
int string_index;

for( string_index = 0; string_index < CSIZE; string_index++ )
  temp->sarray[string_index] = NULL;
...
temp->sarray[0] = malloc( sizeof( char ) * string_length );
...
free( temp->sarray[0] );
It sounds like that is what you were planning to do anyway, but I wanted to reinforce the point in case anyone else finds this thread helpful.

Last edited by Dark_Helmet; 10-30-2006 at 11:30 PM.
 
Old 10-30-2006, 11:49 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Im sorry I actually dident set that out right. Its actually only one string not an array of stings so the i ment to say an array of charecters but got all toung twisty.

anyway the orignial thing i typed was correct. but it still fails to compile.
 
Old 10-31-2006, 12:08 AM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Code:
temp->sarray = NULL; /* should point the first pointer of the array to NULL */
I'm not sure I understand your logic. Why are you doing this?

There are no pointers there. sarray is a char array, not a char pointer or an array of char pointers. You cannot assign NULL to it.

gcc is complaining about incompatible types, because NULL (a pointer) is incompatible with a char array.
 
Old 10-31-2006, 12:22 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
In that case, the compiler is correct, you have a problem with the assignment.

When you declare an array with a definite size, you cannot assign the array itself to NULL. If you want the array to contain the NULL string, assign NULL or 0 to the first character element:
Code:
temp->sarray[0] = NULL; /* or " = 0;" if you prefer */
Then, if you want to change sarray's contents, use strcpy() or any of its counterparts. Just keep in mind that sarray is a string of maximum length 99 (99 "real" chatacters plus a NULL terminator). You cannot expand or shrink that size; you cannot use malloc() for sarray or you'll run into the same error you're getting now by trying to assign NULL.
 
Old 10-31-2006, 10:29 AM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Thanks dark thats actually what im trying to do is just make the string contain the null. Reason i was calling it a pointer is cause the first element in an array is supposed to be a pointer to where the array is in memory. I figured I could just treat the first pointer like any other pointer and assign it null. But a terminating \0 is fine as all im tring to do is just leave blank information in the array.

In my main program the char array is used to store the name of another file on the disk but its not used every time so I needed a way put blank info there. I could just leave it random but I suspect that will cause issues later.

Thanks for all the great help and I'm sorry I dident word that quite right.
 
Old 10-31-2006, 11:33 AM   #7
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
yes the string array is actually a static pointer. And so, it "points" to the first element of the array. But, if you want to assign a value to an element, you should dereference the pointer, otherwise you will be trying to modify where the pointer points to, insted of its value.
So you should have written:

Code:
*(temp->sarray) = '\0';
As a personal matter, I dont like using NULL as '\0' (which is the same value) because its not that clear. What you are doing is putting the string delimiter caracter, not pointing to NULL.

Hope this is useful.
Cheers!
 
Old 10-31-2006, 01:00 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
yea i relize my mistake. I was trying to assign the memory location of the pointer somewhere else when I was really trying to just change the value of the first element. and since its static assiging it somewhere else wouldent make alot of sense cause the computer wouldent know how to go back when you wanted to add data.
 
  


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
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
JavaScript:: alert(node) shows null, but node != null taylor_venable Programming 1 05-01-2006 09:51 PM
fstab-sync: error: libhal_ctx_init_direct: (null): (null) rpz Linux - Hardware 1 11-01-2005 05:42 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

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

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