Thread: multiple pointer program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    multiple pointer program

    pretty basic, first pointer program i have written. format is for a*b=c.
    a has pointer *x, b has pointer *p, and **y, and c has pointer *q, **r, ***z. using *x, **y, and ***z to print results. it gives me error on line 23. i have tried quite a few different ways of writing the multiplication but i cant figure out how i am supposed to write it. i want x times y to equal z. and then print all 3.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
    int a;
    int b;
    int c;
    int* x;
    int* p;
    int* q;
    int** y;
    int** r;
    int*** z;
    
    
        x=&a;
        y=p=&b;
        z=r=q=&c;
    
    
        printf("Enter 2 multiplicands:\n");
        scanf("%d %d", &x, &y);
        z = (x) * (y);
    
    
        printf("multiplicands and result are: %d %d %d", x, y, z);
    
    
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    pointers are as follows:
    x points to a
    y points to p points to b
    z points to r points to q points to c

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try compiling with warnings on.
    Code:
    y=p=&b;
    That doesn't do what you want it to do. You need:
    Code:
    p = &b;
    y = &p;
    Also, you have to dereference points if you want to get what they point at. You should stop with trying to use *** and ** before you understand *.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Indeed - in your second post you've written statements like "z points to r points to q points to c", but that's not what you code is doing. I suggest having 1 assignment to set up each pointer, even if you could put them all on the same line. quzah has given you a couple already....

    At each point in your program, you need to understand how much dereferencing is needed to get to whatever it is you're trying to get to. Your multiplcation for example: currently you're trying to multiply 2 pointers together. Presumably you want to multiply the data items pointed to together. Scribble down the relationships on a piece of paper if you haven't already.

    Another place that needs consideration (but won't give you an error!) is scanf:
    Code:
        scanf("%d %d", &x, &y);
    scanf will read 2 ints and stick them in the memory pointed to by the arguments. So in this case the first int read would be stored at the address pointed to by &x. That is x. That means the scanf will overwrite your pointer x with a value read from stdin! You usually need to take the address of a variable when passing to scanf (with & operator) because you need to pass a pointer. In your code x is a pointer to a, so.... do you need the & operator?

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ok, how else can i point to a pointer? im a bit confused. the assignment is as follows:

    Write a program that creates the structure shown in the figure below and reads data into a and b using the pointers x and y. The program then multiplies the value of a by b and stores the results in c using the pointers x, y, and z. Finally, it prints all three variables using the pointers x, y, and z.


    z
    |
    y r
    | |
    x p q
    | | |
    a b c



    x y and z point to a b and c. b also has p pointer and c also has q then r pointer i guess is how i should say it.
    this program works fine. the only error i get it for the multiplication line...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
    int a;
    int* p;
    int** q;
    int** r;
    int*** s;
    int*** t;
    int*** u;
    int*** v;
    
    
        printf("Enter a value:\t");
        scanf("%d", &a);
    
    
        p=&a;
        q=r=&p;
        s=t=&q;
        u=v=&r;
    
    
        printf("\nValue is: %d\t\n", ***u);
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ok so written like this, they are all seperate. i am not sure what you mean by deferencing my points though. i need to use x p and q instead of x y and z?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
    int a;
    int b;
    int c;
    int* x;
    int* p;
    int* q;
    int** y;
    int** r;
    int*** z;
    
    
        x=&a;
        p=&b;
        y=&p;
        q=&c;
        r=&q;
        z=&r;
    
    
    
    
        printf("Enter 2 multiplicands:\n");
        scanf("%d %d", *x, **y);
        z = x * y;
    
    
        printf("multiplicands and result are: %d %d %d", *x, **y, ***z);
    
    
    
    
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, what are you trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tgarrisoniv View Post
    i am not sure what you mean by deferencing my points though.
    If you don't understand what dereferencing is, then you shouldn't be trying to use pointers-to-pointers-to-pointers-to type.

    Before you do anything else, you should probably read this: Pointers in C - Tutorial - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-04-2011, 10:49 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. ssh to multiple machines via c program?
    By purest in forum Linux Programming
    Replies: 6
    Last Post: 12-12-2006, 11:25 AM
  4. Multiple problems with my program
    By Seirei in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 10:15 PM
  5. single File Pointer to Multiple Files
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 12:49 AM