2

Let's say I have a fixed size 3 of array that stores the RGB of color. Example:

color[3] = {0, 0, 255}

and I have another array, arrayOfColors that stores many colors. Example:

arrayOfColors = { {0, 0, 255}, {0, 0, 0}, {255, 255, 255} } 

I'm not sure what is the best way to do this but I've tried something. But I have errors when I do this approach. Please help me out, I'm very new to C language. Thank you in advance!

unsigned char color1[3] = {0, 0, 0};
unsigned char color2[3] = {0, 255, 255};

unsigned char *rowColors = NULL;
rowColors = (unsigned char*)malloc((2) * sizeof(char));
rowColors[0] = color1;
rowColors[1] = color2;

3 Answers 3

1

Here

unsigned char *rowColors = NULL;

rowColors is a unsigned character pointer i.e rowColors[0] is a single character and a character can not hold character buffer like below

rowColors[0] = color1; /* rowColors[0] is a char, it can't hold char buffer color1 */
rowColors[1] = color2;

If you wish to store multiple character buffer like color1 and color2 into rowColors, it should either of unsigned char** type or array of pointers not just unsigned char* type.

Sample code

unsigned char color1[3] = {0, 0, 0};
unsigned char color2[3] = {0, 255, 255};

unsigned char *rowColors[2]; /* USE ARRAY OF POINTERS */
for(int row = 0;row < 2; row++) {
    rowColors[row] = malloc(MAX_EACH_BUF_SIZE)); /* ALLOCATE MEMORY FOR EACH POINTER FIRST, define MAX_EACH_BUF_SIZE value accordingly */
}
/* AND THEN STORE INTO ARRAY OF POINTER */
rowColors[0] = color1;
rowColors[1] = color2;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is the answer that I was looking for :), sorry I can't upvote your post because I don't have enough reputation points
@Thomas I'm glad about the fact that my answer helped you. You are welcome on Stack Overflow.
1

You can do using C structure. This doesn't describe the process of declaring an array inside another array but it will serve your purpose.

You need to declare a structure type object first. Then make an array of that structure. See bellow example for better understanding:

struct RGBColor
{
  unsigned int R, G, B;
};

RGBColor arrayOfColors[] = { {0, 0, 255}, {0, 0, 0}, {255, 255, 255} };

int main ()
{
  int len = sizeof (arrayOfColors) / sizeof (arrayOfColors[0]);
  int i;
  for (i = 0; i < len; i++)
    {
      printf ("{%d, %d, %d}\n", arrayOfColors[i].R, arrayOfColors[i].G,
          arrayOfColors[i].B);
    }
  return 0;
}

Output:

{0, 0, 255}
{0, 0, 0}
{255, 255, 255}

3 Comments

The returned type from sizeof() is a size_t, not an int
This answer is missing the statement: #include <stdio.h>. Suggest not expecting a beginner in C to know this
@kamolHasan thank you! but if I want to send the arrayOfColors with the struct RGBColor type to other processor, what datatype should I pass in? For example: MPI_Send(arrayOfColors, 3, ?DATATYPE?, 0, 0, MPI_COMM_WORLD);
1

Remember that unless it is the operand of the sizeof or unary & operators, array expressions like color1 and color2 will be converted (“decay”) from expressions of type “N-element array of T” to “pointer to T”, and the value of the expression will be the address of the first element of the array. So in an expression like

x = color1;

the type of x needs to be unsigned char *. So you can start by setting up an array of unsigned char * to store the addresses of separate arrays like color1 and color2:

unsigned char *colors[2];

colors[0] = colors1;
colors[1] = colors2;

However, what might ultimately be simpler is just setting up an Nx3 array of unsigned char:

unsigned char colors[N][3];  // for some N

memcpy( colors[0], color1, sizeof color1 );

To allocate it dynamically, use

unsigned char (*colors)[3] = malloc( sizeof *colors * N );

and you can access each element like a regular array:

colors[0][0] = color1[0];

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.