Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pointer in structure

Posted on 2000-04-19
5
Medium Priority
?
215 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses

810 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