Reputation: 21
I am a C beginner, and I am getting an error when compiling the following:
typedef struct myStructType {
uint8_t myArray[6];
uint8_t x;
}myStruct;
myStruct *pmyStruct;
(*pmyStruct).myArray={1,2,3,4,5,6}; //getting error here
(*pmystruct).x=3;
The error is as follows: incompatible types when assigning to type ‘uint8_t[6]’ from type ‘uint8_t *’
Upvotes: 2
Views: 1927
Reputation: 2322
If you want to keep things simple at first, and you don't want to deal with malloc
and free
yet, you can also first create a struct-object, then fill it with data, and then set a pointer to it:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct myStruct {
uint8_t myArray[6];
uint8_t x;
} myStruct_t;
int main()
{
//create obj
myStruct_t myObj;
//fill it with data
myObj.myArray[0] = 1;
myObj.myArray[1] = 2;
myObj.myArray[2] = 3;
myObj.myArray[3] = 4;
myObj.myArray[4] = 5;
myObj.myArray[5] = 6;
myObj.x = 3;
//set a pointer to it
myStruct_t *pObj = &myObj;
return 0;
}
Upvotes: 0
Reputation: 10381
An array can be initialized when it is declared, so you could have:
int myarr[] = {1,2,3,4,5,6};
but you can't do this with it being a member of your struct.
For your purposes here just initialize it using a loop
int i;
for (i = 1;i<7;++i)
pmyStruct->myArray = i;
(noting that (*pmyStruct).myArray
can use the arrow notation pmyStruct->myArray
)
For more general sets of numbers memcpy()
can be used instead of the for loop
(as two of the other posters noted, you must allocate space for your struct before doing any of this, and deallocate that space when you are finished)
Upvotes: 0
Reputation: 108968
As you are using C99 (the uint8_t
and // comment
say so), you can try compound literals
#include <inttypes.h>
#include <stdlib.h>
struct myStructType {
uint8_t myArray[6];
uint8_t x;
};
int main(void) {
struct myStructType *pmyStruct;
pmyStruct = malloc(sizeof *pmyStruct);
if (pmyStruct) {
*pmyStruct = (struct myStructType){{1, 2, 3, 4, 5, 6}, 3}; // <== THIS
free(pmyStruct);
}
return 0;
}
Upvotes: 2
Reputation: 2876
There are quite a few issues here.
First of all, make sure you have #include <stdint.h>
in your file because some platforms will have an issue with uint8_t.
Secondly, when you create a struct pointer, you must allocate space for that struct. This can be done with the following code:
myStruct *pmyStruct = malloc(sizeof(struct myStructType));
As others have mentioned, you can only use {1,2,3...}
when initializing the array. So to add elements to the array, you need:
for (int i = 0; i < 6; i++)
pmyStruct->myArray[i] = i+1;
When you have a struct pointer, you access it's elements with the ->
operand. So, your last line would be:
pmyStruct->x=3;
and finally, you need a main method. Putting it all together:
#include <stdlib.h>
#include <stdint.h>
typedef struct myStructType {
uint8_t myArray[6];
uint8_t x;
}myStruct;
int main()
{
myStruct *pmyStruct = malloc(sizeof (struct myStructType));
for (int i = 0; i < 6; i++)
pmyStruct->myArray[i] = i+1;
pmyStruct->x=3;
return 0;
}
Once you're done with everything, be sure to free the allocated space with free(pmyStruct);
And pay attention to capitalizations! pmystruct
is NOT the same as pmyStruct
.
Best of luck.
Upvotes: 2