Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to malloc a struct char* element

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
hi,

i have a struct:

Code:
struct list_el
{
        char *sitename;
        struct list_el * next;
};

typedef struct list_el site;

when i create linked list element i do this:

Code:
current_site = (site *)malloc(sizeof(site));

what is the proper way to set the char*? is it:

Code:
current_site->sitename = malloc(length * sizeof(char));
memset(current_site->sitename,'\0',length);
strcpy(current_site->sitename,temp);

please assume temp and length have been initialised properly.

TIA, rotis23
 
"please assume temp and length have been initialised properly"
I have to assume for temp, I do not need for length :)

Code:
current_site->sitename = malloc((length(temp)+1) * sizeof(char));
if (current_site->filename == (char *)NULL) exit(1);
strcpy(current_site->sitename, temp);

My complete answer is in thread thread205-348161 :)

 
thanks dchoulette,

you've confirmed what i thought.

i'm not sure about your row[0]+8 part of the code.

does this add 8 to the size (in which case thats not what i'm doing) or,

copy from position 8 (in which case i want to buy you a drink).

thanks again, rotis23
 
It start copying from the 8th character of row[0].

Note: the examples refer to the above mentionned thread.

In C any char pointer can be used as a string. The string goes from the character pointed to, up to the first null character.
If row[0] is a string (that means than row is an array of strings i.e. a char **) then row[0] is a pointer to the first character of the string row[0] (which is row[0][0]).
And row[0]+8 (pointer arithmetic) is a pointer to the 8th character of the string row[0] (which is row[0][8]) and thus can be seen as a string with just the end of row[0]. (But this only if the length of row[0] is greater or equal to 8 else you are past the end of the string.)

Code:
char *row[] = { "hostinfoHello" };
char *sitename = row[0] + 8;

printf("length(\"%s\") => %d\n", row[0], length(row[0]));
printf("length(\"%s\") => %d\n", sitename, length(sitename));

Should display:
Code:
length("hostinfoHello") => 13
length("Hello") => 5

Beware that row[0] and sitename share the same characters, they just points to different characters of the same string. Any change in characters pointed by sitename will be reflected in the last part of row[0].
(Such a change is unlikely in my example because row[0] is a litteral string, so its characters should/could be stored by the linker in a read only memory segment resulting in a segmentation fault at any attempt to overwrite them)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top