• C

Array pointer

hi,

i hav an array like this:

char *arr[1000];

my problem is that i dont know the size at that time, i would know later where i would malloc it...

but when i do like this: char *arr[]; i get error:

is there a way to declre without putting size init


zizi21Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ozoConnect With a Mentor Commented:
char **arr;
arr = (char **)malloc(1000*sizeof(char *));
0
 
josgoodCommented:
You could
   int* arr;
   int abc;
   int size = 1000;
   arr = malloc(1000 * sizeof(int));
and then reference it by subscript, as in
   arr[0] = 7;
   abc = arr[0];
0
 
spoxoxCommented:
Do you need a pointer to a pointer? (An array is basically a pointer.) If so, go ozo:

  char **arr2;
  arr2 = (char **)malloc(1000*sizeof(char));

  *arr2 = "There are more things in heaven and earth, Horatio, than are dreamt of in your philosophy.";
  printf("%c", (*arr2)[2]);


If not, josgood is good:

   char* arr;
   char abc;
   char size = 1000;
   arr = malloc(1000 * sizeof(char));
   arr = "Now is the winter of our discontent.";
   abc = arr[5];
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
zizi21Author Commented:
hi , i need an array of pointers ..for example
arr[0]="this is";
arr[1]="another string";


0
 
josgoodCommented:
spoxox, its always a pleasure to meet someone who can quote from Richard III and Hamlet!
0
 
zizi21Author Commented:
i have tried like this:

arr=(char **)calloc(1000,sizeof(char*));
and i get bus error...
or arr=(char **)malloc(1000*sizeof(char));
0
 
spoxoxCommented:
;)
0
 
josgoodConnect With a Mentor Commented:
You could do this...
   char** arr;
   arr = (char **)malloc(1000*sizeof(char));
   arr[0] = "Twiddle Dee and Twiddle Dum\n";
   arr[1] = "I've often believed as many as 6 impossible things before breakfast!\n";
   printf(arr[0]);  
   printf(arr[1]);

(with nod to Ozo)
0
 
ozoCommented:
where do you get bus error?
You must still initialize arr[0] before referencing arr[0]
0
 
spoxoxConnect With a Mentor Commented:
OK, further with ozo:


char **arr2;
arr2 = (char **)malloc(1000*sizeof(char));
 
arr2[0] = "She should have died hereafter;";
arr2[1] = "There would have been a time for such a word.";
 
printf("%c", arr2[0][0]);  // S
printf("%c", arr2[1][0]);  // T

Open in new window

0
 
zizi21Author Commented:
thanks:-)
0
 
zizi21Author Commented:
thanks..i would post another question for bus error since it is not related...it works the other way..let me try with bus error...thanks
0
 
zizi21Author Commented:
i am no longer getting bus error..thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.