Let's remove the typedef and see the code
Code:
int main( int argc, char* argv[]){
int arrayLength = 41;
char **table;
table = malloc(arrayLength*sizeof(char*));
char* **tablet = &table; /* Three pointers!!!! */
table[40] = "this";
printf("%s", *tablet[40]);
return 0;
}
I think that you are a little confused and I - std10093 is just a student,which has not the experience of the other forum's users - do not think you are doing it as you should
First segmentation fault is in these cases caused by accessing memory that does not belong to your program!
My guess it that you want create an array which will store the string "this" inside it.I would it like this
Code:
#include <stdio.h>
int main( int argc, char* argv[]){
char* p; /* Declare a pointer to char */
p = "this"; /* Set the pointer to string */
/* Remember that you can modify the pointer, but not the contents */
/* of where it points to.Here you can not modify the second letter */
/* of string "this" */
printf("%c\n",p[1]); /* I can access the first letter */
p[1]= 'a'; /* Error. I got a bus error */
printf("%s\n",p); /* Print the string that pointer p points to */
return 0;
}
On the other hand it seems that you wanted to allocate a 2D array of chars, but i don't see why.
I will make a simple example with type int, because if i make it with char you will lose all the fun
Remember that a 2D array is an 1D array (like the usual ones),where every element of it, is a pointer to another array
Code:
/* Argument n will be the size of our array. */
/* The array is going to be a square one (n x n) */
int **get(int n) /* Malloc the array */
{
int i,**table;
/* Malloc an 1D array,where every element of it is a pointer */
table=malloc(n*sizeof(int *));
/* Go to every element of the 1D array, and because every */
/* element is a pointer,set every one of them to point to */
/* an array (1D), where every element of them is just an int */
/* In the previous malloc they were pointers to int */
for(i=0 ; i<n ; i++)
table[i]=malloc( n*sizeof(int) ); /* *(table+i) is equivalent to table[i] */
return table;
}
Hope this helps
You typedef'ed the char pointer..hmm...i do not know if this is safe in a large program.Maybe you forget that is actually a pointer