Thread: Simple pointer question

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    Question Simple pointer question

    Isnt this how I would dereferrence a pointer

    Code:
    int *num;
    ...
    ...
    int i;
    i = #
    and to assign the value in num to i

    Code:
    i = **num;
    Last edited by sweets; 11-16-2004 at 07:20 PM.
    simple is always an understatement.....

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    An example:
    Code:
    int* p = new int;
    *p = 3; // Assign 3 to the space pointed to by p
    int num = *p; // num == 3 now
    
    or...
    
    int num = 3;
    int* p = # // p now points to num
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    no, it would be
    Code:
    int *num;
    ...
    ...
    int i = 5;
    num = &i;
    ...
    ...
    int x = *num;  //to assign value POINTED TO by num to x
    Just Google It. √

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

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    No, it's like this
    Code:
    int *num;
    int i;
    //The memory adress of i is stored in num.
    num = &i;
    and
    Code:
    //The memory contents pointed by num are dereferenced ONCE and placed in i
    i = *num;

    //EDIT: UH!! fast posters

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    What about :

    Code:
    int *num;
    int i;
    
    num.val=10;
    i=num.val;
    where val is a variable within a class
    simple is always an understatement.....

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    a) num is a pointer to an int
    b) the syntax is wrong

    Code:
     struct foo
     {
        int val;
     };
     
     int main()
     {
        foo* bar = new foo;
        bar->val = 2;
        (*bar).val = 3;
     
     delete bar;
     }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    No!
    num is a pointer to an INT.

    Could be
    Code:
    class Int{
        int val;
        Int(int n=0) : val(n){}
    };
    
    Int num;
    int i;
    
    num.val=10;
    i=num.val;
    //edit: post beated again!
    Last edited by xErath; 11-16-2004 at 08:52 PM.

  8. #8
    Quote Originally Posted by xErath
    No!
    num is a pointer to an INT.
    Quote Originally Posted by Zach L.
    a) num is a pointer to an int
    What are we debating about?

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Check the times. They posted simultaneously.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Ah, thanks; just noticed.

    I'll keep my eye on post times in the future. It may be the key to the answer


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple pointer question
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 01:05 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM