1. ## array of strings

Hi there!

This is my first post and i would first like to apologize for my bad english if there will be one...

I have a question about a program which i do not know how to make exactly, but it is a fairly simple program...

I would like to make an array, where i would save strings into. But i have no idea how to do that. I was thinking of using malloc,...

2. If you use malloc for each string, then this is an array of char pointers, eg, for ten strings:
Code:
`char *strings[10];`
If you use malloc for the whole thing, then you don't need to hardcode the number of strings:
Code:
```char **strings; // pointer to pointer(s)
int n = 10;  // can set n dynamically at runtime
strings = malloc(n*sizeof(char*));```
For both of those you then still need to malloc each string. However, if you are okay with hardcoding both the number of strings and their length, this is fine:

Code:
`char strings[10][256];`
But don't do that if the array is going to be very large. Eg:
Code:
`char strings[10000][1000];`
Is ~10 MB and that will blow your stack, so you would have to use malloc in both dimensions.

3. so if i would go like:

Code:
```for (i=0;i<10;i++){
array[i]=(char*)malloc(10*sizeof(char));
}```
it would be ok?

4. The way you did it, you would need something else before that.
Either
Code:
```    char *array[10];
for (i=0;i<3;i++)
array[i]=(char*)malloc(10*sizeof(char));```
or
Code:
```    char **array = (char **)malloc(10*sizeof(char *));
for (i=0;i<3;i++)
array[i]=(char*)malloc(10*sizeof(char));```

5. yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

sory if i am asking stupid questions, but i would like to understand...

6. Originally Posted by janeznovak
yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

sory if i am asking stupid questions, but i would like to understand...
It doesn't actually make a difference when you actually use the array.
It's more a difference in where exactly the variable in memory is stored, but that doesn't have to concern you too much i guess. Just when using malloc() u have to remember that you release the memory after you're done with free(), since C needs you to take care of that low level stuff by yourself.

7. Originally Posted by janeznovak
yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

sory if i am asking stupid questions, but i would like to understand...
As naturegirl points out the difference is where the base array of pointers is created. In her first example it is created on the stack and is thus of fixed size. In her second example it is created on the memory heap and can be resized using realloc(). There is no difference in performance and the syntax to use both is the same.

The decision between them is pretty simple...
Q: How many strings do you have to store/load ?
A: 41 ... use the first version
A: "I don't know" ... use the second one.