sorry for the long title, but it pretty much sums it all up. I have a 2 test structures, and in one I have a 2d char array, which is essentially a 1d string list.

However I can't seem to interact with it on the same level as I can a normal 2d char array. Using

char test[][]={"stuff", "stuff2"}; approach won't work, the curly brackets makes it unhappy, removing the curly brackets helps, however from debug tests it seems that nothing gets written.

Can anybody help me assign some data to a 2d char array within this structure?

#include <stdio.h>

typedef struct a{
	int a;
}A;

typedef struct b{
	char ba[25][12];
	int bb[12];
}B;

B test;


int main (){
	test.ba[25][12]="a","b";	
	printf ("%s\n%s", test.ba[0][0], test.ba[0][1]);
//returns null here
	return 0;
}

I get an access violation error if I try to use strcpy so i'm either going outside the array bounds or am trying to write to read only memory.

Huge thanks in advance.

Recommended Answers

All 3 Replies

You can use {} with arrays only when initializing.

char ba[25][12] = {"item[0][0]", "item[0][1]", "item[0][2]",......, "item[0][11]",
                               "item[1][0]", "item[1][1]", "item[1][2]",......, "item[1][11]",
                               .
                               .
                               .
                               "item[24][0]", "item[24][1]", "item[24][2]",......, "item[24][11]",
                             }

After initializing you need to assign it using [][] = value

for(int i  = 0; i < 25; i++) {
    for(int j  = 0; j < 12; j++) {    
        ba[i][j] = "item[" + i + "][" + j + "]";// This string concatination might not work with your compiler
    }
}

So your assigning ba[25][12] = "ab", "ba";

here ba[25][12] is out of array range

should be ba[24][11] = "somthing";

and you are printing ba[0][0] and ba[0][1] which are not been assigned.

Hope You get it.

thank you luckychap for your answer, however I am a bit confused about something.

One can say char a[] is simply a string since a string in C is only an array of chars.

So when one says char a[][] is that not a 1d array of strings?

From your instantiation it seems like to me it treats it like a 2d array of strings.

If you defer initialization of the struct object, you can do it all in one go:

#include <stdio.h>

typedef struct a
{
	int a;
} A;

typedef struct b
{
	char ba[2][12];
	int bb[12];
} B;

int main()
{
    B test = 
    {
        {"a", "b"},
        {1, 2, 3, 4, 5}
    };
    int x;

    for (x = 0; x < 2; ++x) printf("%-2.12s", test.ba[x]);
    putchar('\n');
    for (x = 0; x < 12; ++x) printf("%-2d", test.bb[x]);
    putchar('\n');

	return 0;
}

One can say char a[] is simply a string since a string in C is only an array of chars.

That is only half the story. A string in C requires two parts: an array of character type and a terminating '\0' character. If you have an array of char but not '\0' at the end, it is not a string. A string also does not have to be the char type. C supports two character types for strings where there are string constants and string functions defined. char is the single byte character type and wchar_t is the multibyte character type.

So when one says char a[][] is that not a 1d array of strings?

It is a 2D array of char. If the inner arrays are all strings then it is also a 1D array of strings. You can use it either way as long as the requirements for a string are met.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.