0

Off lately I have taken up the task to understand and learn C. Right now I was learning about structures and pointer arrays. I have a problem. I want to populate a pointer array with values. Below is my code:

           struct profile_t 
          {
           unsigned char length;
           unsigned char type;
           unsigned char *data;
          };

          typedef struct profile_datagram_t
         {
          unsigned char src[4];
          unsigned char dst[4];
          unsigned char ver;
          unsigned char n;
          struct profile_t profiles[MAXPROFILES];       
         } header;

          header outObj;

         int j =0;
         int i =0;

      outObj.profiles[j].data = malloc(10);

      for(i=0;i<10;i++)
          {
         if (j=0)
             {
           outObj.profiles[j][i] = 1 2 3 4 5 6 7 8 9 10;
         }
         else 
             {
        j=1;
         }
      }

      for(i=0;i<10;i++)
          {
        if (j=1)
            {
           outObj.profiles[j][i] = 1 2 3 4 5 6 7 8 9 10;
        }
      }

Is the above approach the way to go or am I completely offtrack. MAXPROFILES is 2 (which means only 0 and 1).

5
  • 3
    Please indent your code sanely. Commented Jul 27, 2011 at 21:02
  • 4
    "MAXPROFILES is just 1 (which means only 0 and 1)": no, it means only 0: outObj.profiles[0] is valid; outObj.profiles[1] is not valid. Commented Jul 27, 2011 at 21:04
  • @pmg: thanks, so if MAXPROFILES is 2, then outObj.profiles[1] is valid. And the way I am initialzing the element...is that the proper way to go?? Commented Jul 27, 2011 at 21:07
  • No, you can't do it that way. You'll have to use another loop. Commented Jul 27, 2011 at 21:11
  • @anon: I do not think so, at least I have never ever seen this kind of initializer... see my code snippet! Commented Jul 27, 2011 at 21:11

3 Answers 3

1

You cannot assign like this

outObj.profiles[j][i] = 1 2 3 4 5 6 7 8 9 10;

outObj.profiles[j] is profile_t instance. outObj.profiles[j].data is char*. I suppose that you want to assign that integers to data. First of all you should allocate memory for both j values.

outObj.profiles[0].data = malloc(10);
outObj.profiles[1].data = malloc(10);

I suggest you to replace your code with loops with something like this

for(i=0;i<10;i++) {
    outObj.profiles[0].data[i] = i+1;
    outObj.profiles[1].data[i] = i+1;
}

The result is same but it is much cleaner to read and understand.

Sign up to request clarification or add additional context in comments.

5 Comments

Was the repetition of the lines in the last code piece a copy and paste error or did you mean to use 0 and 1 respectively, instead of 'j`?
@Rudy it was old copy-paste ;-) Fixed it.
@user it depends on the way you get these numbers. If they are stored anywhere you should copy them one-by-one. If you are getting them as a result of some function you should call it inside of the cycle and generate numbers one-by-one either....
@tyz: Thanks for pointing me in the right direction, but as a curious learner of "C", suppose instead of 2 MAXPROFILES, i had 50, don't you think it is cumbersome to write outOBJ.profiles[0], then [1]...[50]...won't a loop be beneficial???
@user Yes, if there are a lot of objects loop is a necessity.
0

Or in short:

#define DATALENGTH 10

uint actProfile = 0;
uint actData = 0;



for( actProfile = 0; actProfile < MAXPROFILES; ++actProfile )
{
    outObj.profiles[actProfile].data = malloc(DATALENGTH);

    //skip check if malloc was successfull...

    for ( actData = 0; actData < DATALENGTH; ++actData )
    {
      outObj.profiles[actProfile].data[actData] = actData +1;
    }
}

1 Comment

:If the data can be anything...not necessarily 1,2,3,4,.....then my code should work right??
0

You don't allocate space for outObj.profiles[1].data.

The sequence of numbers (1 2 ... 10) is a syntax error. You may want to use the loop variable

for (i = 0; i < 10; i++) {
    outObj.profiles[0][i] = i + 1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.