Solved

Pointer in structure

Posted on 2000-04-19
5
194 Views
Last Modified: 2012-05-04
Hello C Gods

I am a newbie to code and would like some help with a small program;

#include <stdio.h>

struct msg {
            char *p1;
            char *p2;
           )myptrs;
myptrs.p1 = "I love C";
myptrs.p2 = "Whatever";

printf("It is true", myptrs.p1);
printf("I can say", myptres.p2);

return 0;
}

Seemed simple enough. But I get the following errors at the "myptrs.p1 and p2 sections:

Data type mismatch in redeclaration
Declaration syntax error

I looked into some books. I can't seem to find my mistake.

Any suggestions?
0
Comment
Question by:pyramid
5 Comments
 
LVL 1

Expert Comment

by:MT_MU
ID: 2731753
your code w/o typos.

#include <stdio.h>

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

int main(void)
{

      myptrs.p1 = "I love C";
      myptrs.p2 = "Whatever";

      printf("It is true", myptrs.p1);
      printf("I can say", myptrs.p2);

      return 0;
}


0
 
LVL 1

Expert Comment

by:mournblade
ID: 2731929
the printf calls should be

printf("It is true %s", myptrs.p1);
printf("I can say %s", myptrs.p2);

the %s indicate you're printing a string
0
 
LVL 1

Accepted Solution

by:
zx9r earned 50 total points
ID: 2732781
The correct code would be this:

#include <stdio.h>

void main() {

                      struct msg {
                                  char *p1;
                                  char *p2;
                      } myptrs;
                      myptrs.p1 = "I love C";
                      myptrs.p2 = "Whatever";

                      printf("It is true %s", myptrs.p1);
                      printf("I can say %s", myptrs.p2);

                      return;
}
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2732981
MT_MU's code is correct (except for the missing %s that I pointed out). the struct and variable myptrs ca be declared outside of the main fcn. however, as zx9r's code shows, it is better to have the variable declared inside a fcn. global variables should only be used for performance's sake, since they can be the cause of programming errors.
0
 
LVL 1

Author Comment

by:pyramid
ID: 2737807
Thanks, this is the answer that I arrived at as well. It still seems to me though I could make myptrs.p1 and .p2 global and it still should work. But whatever works efficiently is good for me.

thanks for the help...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now