I am using visual C++ 6.0 to practice pointer staff, the following is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char first_name[50];
char middle_name[50];
char last_name[50];
int height;
int weight;
}people;
int main()
{
//declare a people structure pointer
people *ppl_ptr;
//allocate memory space for people structure
ppl_ptr = malloc(sizeof(people));
//check if you run out of the heap
if(ppl_ptr==NULL)
{
printf("Out of memory!\n");
return 0;
}
//initialize the unname people varible
(*ppl_ptr)={"bla","bla","bla",192,58};
return 0;
}
but I got a syntax error:
C:\C Practice\ch14.c(30) : error C2059: syntax error : '{'
Error executing cl.exe.
and if I replace
(*ppl_ptr)={"bla","bla","bla",192,58};
with something like
(*ppl_ptr).first_name="bla";
I will get a different error:
ch14.c
C:\C Practice\ch14.c(30) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.
and this error msg doesn’t really make sense to me … does this ever occur to anyone of you? Please let me know why it is giving me this error msg. Thanks in Advance.
Try accessing the structure properly:
You could also write an initialization function with this signature: