Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers as structure 1

Status
Not open for further replies.

cat5ive

MIS
Dec 3, 2004
184
US
Hi,

I need help again. This wouldn't compile. The compiler pointed to statement in red. The error msg said "parse error before '.' token.
thanks in advance

#include <stdio.h>

struct msg {
char *p1;
char *p2;
} myptrs;

myptrs.p1 = "teach yourself C.";

myptrs.p2 = " by sams publishing.";


int main()
{
printf("%s\n", myptrs.p1);

system("PAUSE");
return 0;
}
 
If you mean to use an initialization rather than assignment, use an initialization rather than assignment. If you mean an assignment, do so in a function. If you think you are copying strings, post clarifying follup questions. And please use [code][/code] tags to post code.
 
Code:
#include <stdio.h>
#include <stdlib.h>


struct msg{
        char *p1;
        char *p2;
} myptrs;

[red]        //myptrs.p1 = "teach yourself C.";
        //myptrs.p2 = " by sams publishing.";[/red]



int main()
{
[green]        myptrs.p1 = "teach yourself C.";
        myptrs.p2 = " by sams publishing.";[/green]

        printf("%s\n", myptrs.p1);

        system("PAUSE");
        return 0;
}
 
Thank you for the help. I'm trying to understand how it work.
1. So, if I assign the value, I have to do it inside the function. correct?
2. but the initialization can be done outside the function, right?
3. I thought my 2 statements were initialization. How come they are not?

Thanks.
 
Initialise is
Code:
typename varname = value;

Assignment is
Code:
varname = value;

So your original as initialisation would be
Code:
struct msg {
    char *p1;
    char *p2;
} myptrs = {
    "teach yourself C.",
    " by sams publishing."
};

--
 
In C, and more so in C++, it is an important distiction.

Initialzation happens when the "object" is created, in C++ for an ADT this will call a copy constructor function.

Assignment happens when you already have a varible created and you give it a new value.

The problem with the first bit of code:

Code:
#include <stdio.h>

struct msg {
    char *p1;
    char *p2;
} myptrs; //DECLARATION

[red]
// ASSIGNMENT (cannot happen here)
// These don't create named objects, 
// and so this isn't a initalization of an object,
// it is just a coupole of instructions -- you can't
// have instructions (besides initalizations in the global
// scope. So I moved them to main and left it an assignment
[/red]
myptrs.p1 = "teach yourself C.";
myptrs.p2 = " by sams publishing.";

    
int main()
{
  printf("%s\n", myptrs.p1);
  
  system("PAUSE");    
  return 0;
}

So, I kept it assignment:
Code:
#include <stdio.h>
#include <stdlib.h>


struct msg{
        char *p1;
        char *p2;
} myptrs;

int main()
{
        [green]
        // ASSIGNMENT
        // myptrs already exsits, 
        // and now we give it a value
        [/green]
        myptrs.p1 = "teach yourself C.";
        myptrs.p2 = " by sams publishing.";

        printf("%s\n", myptrs.p1);

        system("PAUSE");
        return 0;
}

And Salem's code shows how to do an initialize a variable.

Code:
struct msg {
    char *p1;
    char *p2;
} myptrs = {
    "teach yourself C.",
    " by sams publishing."
};
[green]
// INITIALIZATION
// This creates a new msg called myptrs with the value on
// the Right Hand of the equals.   The compiler does
// something different than it does with an assignment.
[/green]
 
Thank you for a very clear explaination. You guys are so nice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top