Thread: Newbie question abouty accessing data in array of pointers..

  1. #1
    Registered User
    Join Date
    Aug 2021
    Posts
    3

    Newbie question abouty accessing data in array of pointers..

    In the following code:

    Code:
    unsigned int scale_parameter1[12]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter2[6]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter3[5]= {
     {0x23DF,0x0000,0x1234,0x0000,0x0000} };
    unsigned int scale_parameter4[5]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter5[7]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    
    unsigned int const *scale_parameters[] = 
    { 
     scale_parameter1, scale_parameter2, scale_parameter3, 
     scale_parameter4, scale_parameter5
    };
    I cannot figure out the needed code (for example) to read the 3rd data (0x1234) of variable named scale_parameter3[] using scale_parameters pointer array..

    The following code works to read scale_parameter3[] first data (0x23DF):
    temp_int = scale_parameters[20][0];

    but temp_int = scale_parameters[20][2]; does not work..

    I'm sure it's an easy one for you guys..
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Where did you get 20 from?

    scale_parameters[2][2] is what you want.

    > The following code works to read scale_parameter3[] first data (0x23DF):
    > temp_int = scale_parameters[20][0];
    If it works for you, it's only by dumb luck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2021
    Posts
    3

    [SOLVED] Newbie question abouty accessing data in array of pointers..

    My code looks weird even written between the CODE brackets.. Hmmm.
    Last edited by jpdesroc; 08-08-2021 at 01:22 PM.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by jpdesroc View Post
    In the following code:

    Code:
    unsigned int scale_parameter1[12]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter2[6]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter3[5]= {
     {0x23DF,0x0000,0x1234,0x0000,0x0000} };
    unsigned int scale_parameter4[5]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000} };
    unsigned int scale_parameter5[7]= {
     {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000} };
    
    unsigned int const *scale_parameters[] = 
    { 
     scale_parameter1, scale_parameter2, scale_parameter3, 
     scale_parameter4, scale_parameter5
    };
    I cannot figure out the needed code (for example) to read the 3rd data (0x1234) of variable named scale_parameter3[] using scale_parameters pointer array..

    The following code works to read scale_parameter3[] first data (0x23DF):
    temp_int = scale_parameters[20][0];

    but temp_int = scale_parameters[20][2]; does not work..

    I'm sure it's an easy one for you guys..
    Thanks
    Although the question has been answered can I just point out that that is an eye sore to any programmer, would be easier to read if you did this:
    Code:
    unsigned int scale_parameter1[12] = {0};
    unsigned int scale_parameter2[6] = {0};
    unsigned int scale_parameter3[5] = { 0x23DF, 0, 0x1234, 0, 0 };
    unsigned int scale_parameter4[5] = {0};
    unsigned int scale_parameter5[7] = {0};
    
    unsigned int const *scale_parameters[] = { scale_parameter1, scale_parameter2, scale_parameter3, scale_parameter4, scale_parameter5 };

  6. #6
    Registered User
    Join Date
    Aug 2021
    Posts
    3
    Quote Originally Posted by awsdert View Post
    Although the question has been answered can I just point out that that is an eye sore to any programmer, would be easier to read if you did this:
    Code:
    unsigned int scale_parameter1[12] = {0};
    unsigned int scale_parameter2[6] = {0};
    unsigned int scale_parameter3[5] = { 0x23DF, 0, 0x1234, 0, 0 };
    unsigned int scale_parameter4[5] = {0};
    unsigned int scale_parameter5[7] = {0};
    
    unsigned int const *scale_parameters[] = { scale_parameter1, scale_parameter2, scale_parameter3, scale_parameter4, scale_parameter5 };
    All the 0x0000 datas are there because they all will be replaced by legit values other than that.
    The number of datas in each arrays is calculated too for future values to be placed there.
    That's why all these 0x0000 in the arrays.
    Hope this explaination makes this coding intentionaly done this way.

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by jpdesroc View Post
    All the 0x0000 datas are there because they all will be replaced by legit values other than that.
    The number of datas in each arrays is calculated too for future values to be placed there.
    That's why all these 0x0000 in the arrays.
    Hope this explaination makes this coding intentionaly done this way.
    Even then, until the values are actually replaced a simple 0 is better, you can always add in the x# after but with what you did not only do you have to do that but you also have to delete the excess 0000 you put in, remember KISS, "Keep it Simple Stupid" and you'll have less issues both in reading you're own code and getting help from others, I admittedly fail to do that myself sometimes though I do try to apply KISS to my code, especially when I'm cleaning it up or about to post it online.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question pointers
    By starternewb in forum C Programming
    Replies: 7
    Last Post: 03-14-2011, 10:04 PM
  2. Accessing elements in an array of pointers to arrays
    By fxtdr79 in forum C Programming
    Replies: 5
    Last Post: 06-05-2010, 09:29 PM
  3. Question about accessing private data when I shouldn't.
    By Mostly Harmless in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2008, 09:05 PM
  4. newbie: pointers to array question
    By cstudent in forum C Programming
    Replies: 5
    Last Post: 04-09-2008, 04:02 AM
  5. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 03:14 AM

Tags for this Thread