ansi C 2 dim array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MannyYamzon
    New Member
    • May 2010
    • 1

    #1

    ansi C 2 dim array

    Hi guys,
    Thanks in advance and any help is greatly appreciated. am having a problem saving into my 2 dim char array, I am trying to find out why does the second time I strcpy the new value to tokenAttributeN ame & tokenAttribute it overrides the value of the first saved values, here's how the code goes: (the display logs are at the bottom)

    char *TaskAndPercent ages[5][2];

    Action4()
    {
    char tokenAttributeN ames[1000];
    char tokenAttributes[1000];

    char temptokenAttrib uteNames[1000] = "name1";
    char temptokenAttrib utes[1000] = "100";

    strcpy(tokenAtt ributeName, temptokenAttrib uteNames);
    strcpy(tokenAtt ribute, temptokenAttrib utes);

    TaskAndPercenta ges[0][0] = tokenAttributeN ame;
    TaskAndPercenta ges[0][1] = tokenAttribute;

    displayvalues() ;

    strcpy(temptoke nAttributeNames , "BBB");
    strcpy(temptoke nAttributes, "99");

    displayvalues() ;

    strcpy(tokenAtt ributeName, temptokenAttrib uteNames);
    strcpy(tokenAtt ribute, temptokenAttrib utes);

    displayvalues() ;

    TaskAndPercenta ges[1][0] = tokenAttributeN ame;
    TaskAndPercenta ges[1][1] = tokenAttribute;



    return 0;
    }

    displayvalues()
    {
    lr_output_messa ge ("The TaskAndPercenta ges[0][0] = %s", TaskAndPercenta ges[0][0] );
    lr_output_messa ge ("The TaskAndPercenta ges[0][1] = %s", TaskAndPercenta ges[0][1] );
    lr_output_messa ge ("The TaskAndPercenta ges[1][0] = %s", TaskAndPercenta ges[1][0] );
    lr_output_messa ge ("The TaskAndPercenta ges[1][1] = %s", TaskAndPercenta ges[1][1] );
    lr_output_messa ge ("The TaskAndPercenta ges[2][0] = %s", TaskAndPercenta ges[2][0] );
    lr_output_messa ge ("The TaskAndPercenta ges[2][1] = %s", TaskAndPercenta ges[2][1] );
    lr_output_messa ge ("The TaskAndPercenta ges[3][0] = %s", TaskAndPercenta ges[3][0] );
    lr_output_messa ge ("The TaskAndPercenta ges[3][1] = %s", TaskAndPercenta ges[3][1] );
    lr_output_messa ge ("The TaskAndPercenta ges[4][0] = %s", TaskAndPercenta ges[4][0] );
    lr_output_messa ge ("The TaskAndPercenta ges[4][1] = %s", TaskAndPercenta ges[4][1] );

    return 0;
    }


    log displayed:
    Action4.c(39): The TaskAndPercenta ges[0][0] = name1
    Action4.c(40): The TaskAndPercenta ges[0][1] = 100
    Action4.c(41): The TaskAndPercenta ges[1][0] = (null)
    Action4.c(42): The TaskAndPercenta ges[1][1] = (null)
    Action4.c(43): The TaskAndPercenta ges[2][0] = (null)
    Action4.c(44): The TaskAndPercenta ges[2][1] = (null)
    Action4.c(45): The TaskAndPercenta ges[3][0] = (null)
    Action4.c(46): The TaskAndPercenta ges[3][1] = (null)
    Action4.c(47): The TaskAndPercenta ges[4][0] = (null)
    Action4.c(48): The TaskAndPercenta ges[4][1] = (null)
    Action4.c(39): The TaskAndPercenta ges[0][0] = name1
    Action4.c(40): The TaskAndPercenta ges[0][1] = 100
    Action4.c(41): The TaskAndPercenta ges[1][0] = (null)
    Action4.c(42): The TaskAndPercenta ges[1][1] = (null)
    Action4.c(43): The TaskAndPercenta ges[2][0] = (null)
    Action4.c(44): The TaskAndPercenta ges[2][1] = (null)
    Action4.c(45): The TaskAndPercenta ges[3][0] = (null)
    Action4.c(46): The TaskAndPercenta ges[3][1] = (null)
    Action4.c(47): The TaskAndPercenta ges[4][0] = (null)
    Action4.c(48): The TaskAndPercenta ges[4][1] = (null)
    Action4.c(39): The TaskAndPercenta ges[0][0] = BBB
    Action4.c(40): The TaskAndPercenta ges[0][1] = 99
    Action4.c(41): The TaskAndPercenta ges[1][0] = (null)
    Action4.c(42): The TaskAndPercenta ges[1][1] = (null)
    Action4.c(43): The TaskAndPercenta ges[2][0] = (null)
    Action4.c(44): The TaskAndPercenta ges[2][1] = (null)
    Action4.c(45): The TaskAndPercenta ges[3][0] = (null)
    Action4.c(46): The TaskAndPercenta ges[3][1] = (null)
    Action4.c(47): The TaskAndPercenta ges[4][0] = (null)
    Action4.c(48): The TaskAndPercenta ges[4][1] = (null)
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please use CODE tags when you post code. That would have provided line numbers that I could refer to.

    Each entry in your 2-dimensional array is a pointer to a string that is stored someplace else. Notice that the strings themselves are not stored in the array. The strcpy's alter the strings (which are stored elsewhere), not the array.

    Look at the last two lines in Action4 before the return statement where you assign values to array entries [1][0] and [1][1]. At this point, both [0][0] and [1][0] point at the same string (tokenAttribute Name); and both [0][1] and [1][1] point at the same string (tokenAttribute ). Were you to call displayvalues just before the return you would get
    Code:
    Action4.c(39): The TaskAndPercentages[0][0] = BBB
    Action4.c(40): The TaskAndPercentages[0][1] = 99
    Action4.c(41): The TaskAndPercentages[1][0] = BBB
    Action4.c(42): The TaskAndPercentages[1][1] = 99
    Action4.c(43): The TaskAndPercentages[2][0] = (null)
    Action4.c(44): The TaskAndPercentages[2][1] = (null)
    Action4.c(45): The TaskAndPercentages[3][0] = (null)
    Action4.c(46): The TaskAndPercentages[3][1] = (null)
    Action4.c(47): The TaskAndPercentages[4][0] = (null)
    Action4.c(48): The TaskAndPercentages[4][1] = (null)
    If you want each array entry to point to a unique string then you need distinct string buffers for each to point at ... something like this:
    Code:
    char buf00[1000];
    char buf01[1000];
    ...
    char buf41[1000];
    Followed by one of the following two lines. I don't trust myself to remember the organization of multidimensiona l arrays, so I always look it up. It is up to you to look it up and choose the right initializer.
    Code:
    char * const TaskAndPercentages[5][2] =
        { {buf00, buf01}, ... {buf40, buf41} };
    <<or>>
    char * const TaskAndPercentages[5][2] =
       { {buf00, buf10, ... buf40}, {buf01, buf02, ... buf41} };
    Notice how I use const here. This makes the pointers constant -- you can't change which string buffers they point at.

    Alternatively, you could include the string buffers in the array. As I mentioned above, I would have to look it up to decide which of these is correct:
    Code:
    char TaskAndPercentages[5][2][1000];
    <<or>>
    char TaskAndPercentages [1000][5][2];
    I don't recommend either of these approaches though. It is much more straightforward to use a one-dimensional array of structures. No need to look up the details of array organization; and no need to remember that [0] is the name and [1] is the attributes.
    Code:
    struct tt {
       char name[1000];
       char attribute[1000];
       };
    struct tt TaskAndPercentages[5];

    Comment

    Working...