Thread: structure pointer?

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    structure pointer?

    heyho
    my compiler gives me a warning but i dont know why:
    Code:
    #include<stdio.h>
    
    void print(struct msg *ptr);
    
    int main()
    {
    	struct msg
    	{
    		int num;
    	} in;
    	int *num;
    	num = &in.num;
    	printf("enter some number: ");
    	scanf("%d", &num);
    	print(num); //compiler warning: suspicious pointer conversion
    	return 0;
    }
    void print(struct msg *ptr)
    {
    	printf("the value is %d", ptr);
    }
    the code works but the warning is making me think.
    do you know why the compiler warns me?

    thanks

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    You've defined num as an "int *", and your print function takes a "struct msg *" as input. Try defining print as having "int *ptr" for input instead of "struct msg *ptr".

    If want the actual value (Not its position in memory) of ptr to be printed, you need to change "ptr" in your printf statement to "*ptr" (dereferencing the pointer).

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    it works now.
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM