ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.