0

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.

2
  • 2
    Tangential note: Visual Studio 6 is 14 years old, and so is seriously out-dated when it comes to supporting newer features of C++. I suggest you upgrade to something modern (Visual Studio 2010 Express is free, and 14 years more up-to-date!). Mar 31, 2012 at 16:00
  • @OliverCharlesworth it is a C question (VC++ 6 has a C compiler and a C++ compiler). We can tell he is using C because he did not get a compiler error for failing to cast malloc!. But upgrading compilers is certainly a good idea, especially considering that recent editions of VS Express are free.
    – M.M
    Dec 8, 2014 at 4:14

4 Answers 4

2

Try accessing the structure properly:

char* bla_str_ptr = "bla";
[...]
strcpy(ppl_ptr->first_name, bla_str_ptr);
strcpy(ppl_ptr->middle_name, bla_str_ptr);
ppl_ptr->height = 123;
[...]

You could also write an initialization function with this signature:

void ppl_init(char* first_name, char* middle_name, [...])
1
  • thanks, I just realize I made a mistake of trying to initialize the string in the wrong way. the correct way should be strcpy(variable_name, "String")
    – shanwu
    Apr 1, 2012 at 13:25
1

In standard C, the = {} syntax is supported for initialization only, not for assignment after initialization. Use a loop to copy into (*ppl_ptr), element by element.

1

In C90 you cannot use { } in assignments; only in initialization (i.e. when providing initial values to a variable in the same command as you are declaring the variable).

In C99 you could write (*ppl_ptr)=(people){"bla","bla","bla",192,58};, but VC++ 6.0 is too old to support that.

What you can do is make another variable using initialization, and then copy using =:

people x = {"bla","bla","bla",192,58}; 
*ppl_ptr = x;
1

Some tips:
1. Always intialize a ponter to null else it may contian garbage value which if unknowingly accessed could cause problem.

people *ppl_ptr = NULL;

2. (*ppl_ptr).first_name="bla";
a) With the malloc, 50 character space is already allocated to it. So if you replace it with the address of "bla", you will be causing memory leak. Luckily, you have the l-value error to avoid such issue.
b) An Lvalue is an expression that can appear on the left-hand side of an equal sign, i.e. something that can be assigned a value.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.