Solved

Array pointer

Posted on 2008-06-22
13
220 Views
Last Modified: 2010-04-21
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


0
Comment
Question by:zizi21
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 21842757
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
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 21842759
char **arr;
arr = (char **)malloc(1000*sizeof(char *));
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21842852
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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


0
 
LVL 13

Expert Comment

by:josgood
ID: 21842872
spoxox, its always a pleasure to meet someone who can quote from Richard III and Hamlet!
0
 

Author Comment

by:zizi21
ID: 21842877
i have tried like this:

arr=(char **)calloc(1000,sizeof(char*));
and i get bus error...
or arr=(char **)malloc(1000*sizeof(char));
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21842881
;)
0
 
LVL 13

Assisted Solution

by:josgood
josgood earned 150 total points
ID: 21842889
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
 
LVL 84

Expert Comment

by:ozo
ID: 21842894
where do you get bus error?
You must still initialize arr[0] before referencing arr[0]
0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 150 total points
ID: 21842900
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
 

Author Comment

by:zizi21
ID: 21842912
thanks:-)
0
 

Author Closing Comment

by:zizi21
ID: 31469609
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
 

Author Comment

by:zizi21
ID: 21842955
i am no longer getting bus error..thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to set environment variables in C 2 86
C hashtable library 3 100
Socket Programming (Unix) 8 141
change colour of repeater control in asp.net c# 7 113
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question