# Thread: Defining an initialized int table?

1. ## Defining an initialized int table?

I'm trying to define an array of pointers to strings of ints each of variable length.
I can easily do this with chars like this
Code:
```char *StringData[]={
"string1",
"string2,
};```
I though maybe something like this would work
Code:
```unsigned int *Data[]={
unsigned int []={0x1, 0x2, 0x0};
unsigned int []={0x0f, 0x02, 0x0};
};```
I don't want to use a 2d array because I will have a lot of int strings and only a few of them will be long which is going to waste a lot of space. I supose I could define all the data in a 1d array and then create a second array of pointers and asign the pointers to different elements of the first array but I was hoping there was a way to do it as simply with with chars.

I apreachiate any thougs.

2. You are going to have to use a 2 dimension array to do that.
Because if you think about it the array of strings is also a 2 dimensional array also it is just initialized differently.

aren't the strings stored one after another and the array is just pointers to them? where as in a 2d array each 'string' would be the size of the longest?

4. Yea but remember a string is an array of characters so technically it is a 2d array still. Even though it is malloced and not a specified length to begin with you would still access a single char this way:

Code:
`string[x][y] = ....`

5. if i get what you mean, you want a single array, whicih has strings in it, and a second is an array of pointers which points to each individual string? in that case, you can try something like
Code:
``` unsigned int array1[]={0x1, 0x2, 0x0};
unsigned int array2[]={0x5,0x7};
unsigned int *ptr[10];
ptr[0]=array1;
ptr[1]=array2;```
sorry if this is not what you meant, im really sleepy to see anything properly..

6. Originally Posted by Quantum1024
I don't want to use a 2d array because I will have a lot of int strings and only a few of them will be long which is going to waste a lot of space.
All ints will take up sizeof(int), whether or not the are "long" or "short"; that is, 2147483647 may take exactly the same space as 0. Adding in the space occupied by the pointers may be fruitless -- could you provide more background on what it is you are trying to do?

7. All ints will take up sizeof(int), whether or not the are "long" or "short"; that is, 2147483647 may take exactly the same space as 0. Adding in the space occupied by the pointers may be fruitless -- could you provide more background on what it is you are trying to do?
he has some int strings which might be long or short..he does not mean long as in the data type..what he means is that he might have an int array which might have 5 elements, and another array which might have 15 elements.

8. PING I'll go with your solution it produces what I wanted and dosen't waste any space.
Thanx to everyone else as well for your thoughts.

9. Code:
```#include <stdio.h>
#include <limits.h>

unsigned int array1[]={0x1, 0x2, 0x0};
unsigned int array2[]={0x5,0x7};
unsigned int *ptr[]  ={array1,array2};

unsigned int array[][3]  = {
{ 0x1,  0x2,  0x0},
{ 0x5,0x7},
};

int main()
{
size_t total = sizeof array1 + sizeof array1 + sizeof ptr;
printf("sizeof array1 = %lu\n", (long unsigned)sizeof array1);
printf("sizeof array2 = %lu\n", (long unsigned)sizeof array2);
printf("sizeof ptr    = %lu\n", (long unsigned)sizeof ptr);
printf("total         = %lu\n", (long unsigned)total);
puts("--");
printf("sizeof array  = %lu\n", (long unsigned)sizeof array);
return 0;
}

/* my output
sizeof array1 = 12
sizeof array2 = 8
sizeof ptr    = 8
total         = 32
--
sizeof array  = 24
*/```
Code:
```#include <stdio.h>
#include <limits.h>

unsigned int array1[]={0x1, 0x2, 0x0};
unsigned int array2[]={0x5,0x7};
unsigned int *ptr[10]={array1,array2};

unsigned int array[][3]  = {
{ 0x1,  0x2,  0x0},
{ 0x5,0x7},
};

int main()
{
size_t total = sizeof array1 + sizeof array1 + sizeof ptr;
printf("sizeof array1 = %lu\n", (long unsigned)sizeof array1);
printf("sizeof array2 = %lu\n", (long unsigned)sizeof array2);
printf("sizeof ptr    = %lu\n", (long unsigned)sizeof ptr);
printf("total         = %lu\n", (long unsigned)total);
puts("--");
printf("sizeof array  = %lu\n", (long unsigned)sizeof array);
return 0;
}

/* my output
sizeof array1 = 12
sizeof array2 = 8
sizeof ptr    = 40 <<edited to be correct>>
total         = 64
--
sizeof array  = 24
*/```

10. I think the extra space used by pointers is worth if for the possible gains when you consider something like.
Code:
```unsigned int array1[]={0x1, 0x2};
unsigned int array2[]={0x1,0x2};
unsigned int array3[]={0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10};```
without using the table of int strings meathod would be defined like so
Code:
`unsigned int array[3][10];`
120 (10*3*4) bytes as aposed to 51 (14+3*4)

11. There is no such thing as an "int string". You may have an array of integers, but it's not an "int string". I don't really see what their doing being very useful anyway, when you can simply allocate it all with malloc just as easy.

Quzah.