Solved

Pointer in structure

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 99
Converting a float to a string function in c languange 10 206
Adjust Mfcapp 29 166
Read file descriptor 3 in C 14 107
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files in the C programming language.

816 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