Pointer to structure problem

This is a discussion on Pointer to structure problem within the C Programming forums, part of the General Programming Boards category; Hey guys i m New to C and i was just learning pointer to structure i need help thanks Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Pointer to structure problem

    Hey guys i m New to C and i was just learning pointer to structure i need help thanks
    Code:
    #include <stdio.h>
    struct cd
    {
    int x;
    int lol;
    };
    read(int *e)
    {
    printf("Please enter number: ");
    scanf("%d",&e->x);
    printf("Number is %d",*e->x);
    
    }
    int main(void)
    {
    int x;
    read(&x);
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suppose read should take a pointer to struct cd, not a pointer to int.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just because struct cd has a member named x doesn't mean that if you have an int x in main it will be the same thing. Here is your code with a couple of changes so that it will compile, and in a more readable format:

    Code:
    #include <stdio.h>
    
    struct cd {
    	int x;
    	int lol;
    };
    
    read(int *e) {
    	printf("Please enter number: ");
    	scanf("%d",e);
    	printf("Number is %d\n",*e);
    }
    
    int main(void) {
    	struct cd mkcd;
    	read(&mkcd.x);
    	printf("Number is %d\n",mkcd.x);
    	return 0;
    }
    I added the line in green, and the \n's...it's just like christmas!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    thanks alot mate and happy christmas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dereferencing a Void Pointer in a Structure
    By simpsonseric in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 03:58 PM
  2. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 03:32 AM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21