GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Aug-2005, 06:01
Hyuga Hyuga is offline
New Member
 
Join Date: Aug 2005
Posts: 14
Hyuga is on a distinguished road

Declaring an array of strings dynamically (C Language)


Hello everybody!

I have restarted to program in C after long years without working with it, and I have a doubt.

I need to declare an array of strings, but I don't know how muchs strings will have to store in this array.

I have declared my array like this, because I want that every position of the array can handle a string of 10 chars.
CPP / C++ / C Code:
unsigned char *arrayIndices[10];

Later, when I know how much array members I need, I tried to allocate memory to handle them:
CPP / C++ / C Code:
arrayIndices=numLines*((char*)malloc(10*sizeof(char)));
numLines is the number of members I need, but this doesn't work for me, and I don't know how to solve that.

Can you help me?
  #2  
Old 03-Aug-2005, 06:16
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Quote:
Originally Posted by Hyuga
Hello everybody!
I have declared my array like this, because I want that every position of the array can handle a string of 10 chars.
CPP / C++ / C Code:
unsigned char *arrayIndices[10];
You have misunderstood. Your code declares array of 10 pointers. These 10 pointers each point to unsigned char. You could 'malloc' arrays of unsigned chars and put the pointers to point at the start of these arrays. I think though that the trad. null terminated c strings should be of type 'char' only instead of 'unsigned char'.

Quote:
Originally Posted by Hyuga
Later, when I know how much array members I need, I tried to allocate memory to handle them:
CPP / C++ / C Code:
arrayIndices=numLines*((char*)malloc(10*sizeof(char)));
numLines is the number of members I need, but this doesn't work for me, and I don't know how to solve that.

Can you help me?
Instead you should do something like the following:
CPP / C++ / C Code:
/* char *arrayIndices[10]; */
for(int i=0; i<10; ++i )
{
    arrayIndices[i] = malloc(11*sizeof(char));  /* 10 characters + '\0' */
}
  #3  
Old 03-Aug-2005, 08:18
Hyuga Hyuga is offline
New Member
 
Join Date: Aug 2005
Posts: 14
Hyuga is on a distinguished road
Quote:
Originally Posted by maprich
You have misunderstood
Totally agree

Quote:
Originally Posted by maprich
Instead you should do something like the following:
CPP / C++ / C Code:
/* char *arrayIndices[10]; */
for(int i=0; i<10; ++i )
{
    arrayIndices[i] = malloc(11*sizeof(char));  /* 10 characters + '\0' */
}
Ok, it worked, thanks maprich. However, now I am trying to understand where I am declaring my X lenght array. Maybe I have to declare each member one by one? (for i=0 to X, reserve space for each 10 character string)
  #4  
Old 03-Aug-2005, 09:01
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Quote:
Originally Posted by Hyuga
However, now I am trying to understand where I am declaring my X lenght array.
You did it in:
CPP / C++ / C Code:
char *arrayIndices[10];
as simple as that. The declaration above declares the array. Also memory is allocated for exactly 10 pointers. But please notice that immediately after above declaration the pointers are not defined. Means that they are pointing randomly and unsafely somewhere in the memory and cannot be used before you allocate memory to each one of them with the malloc.

after above decleration the situation is something like following:

Code:
pointer arrayIndices | V pointer arrayIndices[0] -> not defined (aka garbage) pointer arrayIndices[1] -> not defined ..... pointer arrayIndices[9] -> not defined
that's why you have to allocate memory in for loop to each pointer. As you see above there is no strings at this point. Only after the malloc in the for loop that I posted the situation looks like following:
Code:
pointer arrayIndices | V pointer arrayIndices[0] -> memory of length N allocated with malloc pointer arrayIndices[1] -> aka array ..... pointer arrayIndices[9] -> aka string
so arrayIndices[2] points to the beginning of the 3rd word and arrayIndices[1][4] points to the 5th character of the 2nd word.

Hope that clarified things
  #5  
Old 03-Aug-2005, 09:51
Hyuga Hyuga is offline
New Member
 
Join Date: Aug 2005
Posts: 14
Hyuga is on a distinguished road
Yes, it clarified things. And now I realize that this is not what I need. I need a X lenght array, I don't know X until some point of the program. So what I really need is an array that has one string in every position:
arrayIndices[1] = "string1"
arrayIndices[2] = "string2"
arrayIndices[3] = "string3"
.
.
.
arrayIndices[X] = "stringX".

Can I achieve this, or it is impossible? I mean, can I indicate the lenght of my array programatically?

Maybe I should use this declaration:
char **arrayIndices
indicating that this is an array of char pointers, or something like that.

I hope I'm explaining my problem well....if I'm not, let me know and I will retry
  #6  
Old 03-Aug-2005, 10:18
Hyuga Hyuga is offline
New Member
 
Join Date: Aug 2005
Posts: 14
Hyuga is on a distinguished road
I'm, more or less, in the right path

I've found a solution in this thread: Pointers in C (Part I).

All I have to do is to declare my array as I mentioned
CPP / C++ / C Code:
char **arrayIndices;


And next, when I have the number of files, allocate the necessary space for every string:
CPP / C++ / C Code:
// for the "first level"
arrayIndices = malloc(numLineas * sizeof *arrayIndices);

// In the second level
arrayIndices[indexArray] = malloc(strlen(numIndice) + 1);
strcpy(arrayIndices[indexArray],numIndice);

Where numIndice is the string I want to store, and indexArray is the position index of the array.

Thanks for your help
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 08:46
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 11:24
Creating N string gwk C Programming Language 3 21-Jul-2004 00:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.