pointer

This is a discussion on pointer within the C Programming forums, part of the General Programming Boards category; i have a basic program on pointer here i want to enter a number and get the value a and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    pointer

    i have a basic program on pointer here i want to enter a number and get the value a and b the same. For example
    input 10
    output
    a=10
    b=10

    and it doesnt work is that possible. because if i define int a=10 i will get
    a=10
    b=10

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {    
                                 int a , b;  // if i put int a = 10 this will let me get value a=10 and b=10
                                 int  *ptr;
                                 ptr = &a;
                                 b = *ptr;
                                 printf("enter a");
                                 scanf("%i",&a);
                                 printf("a = %d \n", a);
                                 printf("b = %d \n", b); // i want to get 10 here
    
    
    
           getch();
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Whats your reason for wanting the pointer, are you just trying to learn them?

    That is not going to work because you are assigning b to the value in a. If your print out the value of *ptr that will show the 10 you are looking for.
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    yea iam just trying to learn them. why is that when i printf("%i", *ptr) i= 10. i have asign b to ptr. b=*ptr. it should give me 10 for b. but it gives me a garbage value
    Last edited by mouse666666; 03-31-2010 at 01:04 AM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    The problem is that the *ptr has no values when you assign that to b.So it assigns the garbage value in the *ptr to b.After assigning that only you have get the value of a .Now that a's value will in the *ptr.

    So you need to assign the pointer value to b after getting the input from the user.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    main()
    {
                                 int a , b;  // if i put int a = 10 this will let me get value a=10 and b=10
                                 int  *ptr;
                                 ptr = &a;
                                 // b = *ptr; // Wrong . Assigning the value in the pointer ( But a has no values so it gives the garbage values
                                 printf("enter a");
                                 scanf("%i",&a);
                                 b = *ptr; // Correct . Assigning the value in the pointer ptr.( Because now the *ptr has the value of a ).
                                 printf("a = %d \n", a);
                                 printf("b = %d \n", b); // i want to get 10 here
    
    getch();
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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