Thread: Defining an initialized int table?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    847

    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. #2
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Thanx for your reply

    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. #4
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    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. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    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..
    Last edited by PING; 03-29-2005 at 09:14 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    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.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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
    */
    Last edited by Dave_Sinkula; 03-29-2005 at 10:29 PM. Reason: My bad.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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)
    Last edited by Quantum1024; 03-29-2005 at 11:19 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  2. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM