?
Solved

Pointer in structure

Posted on 2000-04-19
5
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.
Suggested Courses

741 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