c - Structure char array usage -


i have problem understanding how works out :

struct main_s {    char test[1]; }; 

is 2 dimensional array test[1][x] ? example how pass string "hello world" field of structure ?

char array[1][11] = { {"h","e","l","l","o"," ","w","o","r","l","d"} };

and main_s->test = array doesn't work, compiler gives error types, 1 char [] , char*.

how pass string "hello world" field of structure ?

you have first declare sufficient memory space test array contain desired string. "hello world" contains 11 charachters. array should contains @ least 12 elements

struct main_s {    char test[12]; }; 

and copy string array with:

struct main_s m; strcpy(m.test, "hello world"); printf("%s\n", m.test) // display content of char array string 

if want declare 2d array:

struct main_s {    char test[3][13]; }  struct main_s m; strcpy(m.test[0], "hello world0"); strcpy(m.test[1], "hello world1"); strcpy(m.test[2], "hello world2"); printf("%s\n%s\n%s\n", m.test[0], m.test[1], m.test[2]); 

the

strcpy(m.test[1], "hello world1"); 

is equivalent to:

m.test[1][0]='h'; m.test[1][1]='e'; m.test[1][2]='l'; . . m.test[1][10]='d'; m.test[1][11]='1'; m.test[1][12]='\0'; //add null charachter @ end. it's mandatory strings 

the above code not allowed

m.test[1] = "hello world1"; m.test[1] = {'h','e','l','l','o',' ','w','o','r','l','d','1'}; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -