pointer intializing to

This is a discussion on pointer intializing to within the C Programming forums, part of the General Programming Boards category; run this code OK. How do I modify it so I don't have to use the t veriable. Code: int ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    pointer intializing to

    run this code OK. How do I modify it so I don't have to use the t veriable.

    Code:
    int mat[N][N]={{2,4,6,8},{24,8,3,10},{22,1,8,12},20,18,16,14}};
    	int t, *d;
    	t=mat[0][1]-mat[0][0];
    	d=&t;

  2. #2
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by ronenk
    run this code OK. How do I modify it so I don't have to use the t veriable.

    Code:
    int mat[N][N]={{2,4,6,8},{24,8,3,10},{22,1,8,12},20,18,16,14}};
    	int t, *d;
    	t=mat[0][1]-mat[0][0];
    	d=&t;
    You want the address of a variable, but you don't want this variable? Impossible. Be more specific.
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't. t is an integer, which is storing the result of two integers being subtracted. Thus, it give you an integer. If you want a pointer to an integer, you have to have at least one integer to point to. What your code does, in effect is this:
    Code:
    int t, *d;
    t = 4 - 2; /* make t store something, doesn't matter what */
    d = &t; /* make d point to t */
    You have to have an integer for your pointer to point at. In short, all pointers must have something to point at.

    [edit]Curses, foiled again.[/edit]

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

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    to make myself clearer: what I wanted was somthing like this:
    Code:
    int mat[N][N]={{2,4,6,8},{24,8,3,10},{22,1,8,12},20,18,16,14}};
    	int  *d;
    	d=mat[0][1]-mat[0][0];
    what might replace this type of thing? is there?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How about instead of saying what you want with really bad code, you say what you want to do in words.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    mabey
    Code:
    int *d;
    d = malloc(sizeof(int));
    *d  = 4 - 2; /* :) */
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>mat[0][1]-mat[0][0];
    That evaluates to an r-value, or basically _a number_, meaning it isn't stored at any particular memory location that you can point at with a pointer. It's like saying:

    int* d = &2;

    The only solution I can see is something along the lines of what chrismiceli posted, although that is messier and harder to use than the method involving the 'int t;'. In fact, it does pretty much the same thing, except that the variable is allocated on the heap instead of the stack, and d points to the heap-variable which you then have to free() after you're done with it.
    Last edited by Hunter2; 08-08-2004 at 05:24 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Thank you.
    BTW: why can't I find your website? is it down or is it my side problem?
    After a long time of hanging I get "Gateway Timeout".

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It seems to be down That happens once in a while (very annoying), but it usually goes back up in a day or two. Sorry!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  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