0

I have a data type defined by me, and I want to create a matrix of that data type, but I'm not able to use it.

I have typedef char data[10];

data  **matrix;
matrix=(data**)malloc(n*sizeof(data*));
for (i=0;i<x;++i)
    matrix[i]=(data*)malloc(m*sizeof(data));
matrix[i][j]="example";

But in the last line i get an error saying incompatible types, even if I use data of the same type (in this case from a dynamic vector). Is there an error creating the matrix or using it?

Jack
  • 321
  • 1
  • 2
  • 11
  • show us the code of `estado` – Cacho Santa Oct 07 '13 at 20:42
  • why are you using a `data` variable? – fotanus Oct 07 '13 at 20:43
  • 2
    This code doesn't make any sense. In the last line you are assigning a string literal `"example"` to whatever `estado` is defined to be. Probably wrong in any case – Pankrates Oct 07 '13 at 20:48
  • sorry, fixed. i'm using a data type because i want to create a matrix where each element is a small string – Jack Oct 07 '13 at 20:52
  • 2
    There should be an "automatically add ["don't cast the return value of `malloc()`"](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) comment" button under *every question tagged `c`...* ) This is literally the 5th (or so) question today whose author made this mistake. –  Oct 07 '13 at 21:16
  • Thank you for your commen H2CO3, I always see that in the examples, but never understood why. – Jack Oct 07 '13 at 21:22
  • @Jack This confusion perhaps arises out of the misconception that C is a subset of C++, which it isn't. People oftentimes try to compile C code with a C++ compiler, as if it was C++, then try to fix the errors with horrible hacks such as casting away `void *` pointers. Even a lot of books and teachers (!) suggest that. Experts don't. –  Oct 07 '13 at 21:28

2 Answers2

1

Assuming estado is char type.

estado  **matrix = malloc(n*sizeof(char*));  //allocte number of pointers 

for (i=0;i<x;++i);       
matrix[i]=malloc(m); //allocate each pointer
matrix[i]="example";

Assuming estado is int type. This is same for struct.

estado  **matrix = malloc(n*sizeof(int*)); //allocte number of pointers 

for (i=0;i<x;++i); 
{      
matrix[i]=malloc(m *sizeof(int)); //allocate each pointer

for(j=0;j<m;j++)  
matrix[i][j]=1;  or You can also use `memcpy()`  
}
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 1
    In any case, one should probably prefer allocating a real multidimensional array, like this: `char (*arr)[size_y] = malloc(size_x * sizeof *arr);` –  Oct 07 '13 at 21:17
1

Here

matrix[i][j]="example";

you assign to array which is illegal. Try this:

strcpy( matrix[i][j], "example" );

Please note that strcpy is insecure, use more secure alternative for your system - strlcpy or strcpy_s. Or you can follow H2CO3's suggestion.

kotlomoy
  • 1,420
  • 8
  • 14
  • Please `memcpy()` and manually NUL-terminate instead. This will cause buffer overflows very quickly. (This applies to you too, @Jack.) –  Oct 07 '13 at 21:21