Solved

Pointer in structure

Posted on 2000-04-19
5
197 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to connect an iphone application to mysql database 10 186
memory mapped I/O query 6 142
C: GetDiskFreeSpaceEx() for default drive 14 104
UPD maximums on Red Hat 6 109
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

930 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

8 Experts available now in Live!

Get 1:1 Help Now