Thread: help with pointers

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool help with pointers

    I have a hard time understanding pointers in C. can anybody explain me how these operations differ?

    int num = 3;
    int *ptr;

    ptr = #
    ptr = num;
    ptr = *num;

    I just started doing pointers but they seem kind of confusing

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by torquemada View Post
    I have a hard time understanding pointers in C. can anybody explain me how these operations differ?

    int num = 3;
    int *ptr;

    ptr = #
    int num = 3; // variable declaration
    int *ptr; // creating pointer

    ptr = # // storing address of num into ptr

    now if you dereference ptr you can manipulate num
    ex. *ptr=45;
    now num=45


    ptr = num; // not quite sure about this although it might store the address 3 into ptr
    Last edited by camel-man; 10-08-2011 at 10:47 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int num = 3;
    Make an integer type named num, and assign it the value of 3.
    Code:
    int *ptr;
    ptr = #
    Make a pointer to an integer type called ptr, assign that the value of the address of an integer type.
    Code:
    ptr = num;
    That is incorrect, because: =num means "give me the value that num stores", which is an integer, which is not the right type for ptr. The type for a ptr = assignment must be of type int *, not int.
    Code:
    ptr = *num;
    That doesn't work either, because num isn't a pointer, so you can't dereference it.


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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by camel-man View Post
    now if you dereference ptr you can manipulate num
    ex. ptr*=45;
    now num=45
    You have that backwards.
    Code:
    *ptr = 45;
    What you have is multiplying the value of pointer by 45.


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

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    oh yeah sorry typo there

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool

    hmm, I'm still kind of confused. The reason why I asked is because I saw those different types of coding on the book I'm using and some other examples from a professor. Maybe I just took the wrong way. Here parts of the codes where I saw them.

    Code:
    int main(void)
    {
    char board[3][3] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
    };
    char *pboard = *board; /* A pointer to char */
    Code:
    int x = 10;
    int *p1;
    p1 = &x;
    Code:
    int x = 10; 
    int y = 5; 
    int *ptrx = &x; 
    int *ptry = &y;
    *ptrx = *ptrx + 2 
    ptrx = ptry;
    but in this case, ptrx and ptry are both pointers.

    Thanks

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by torquemada View Post
    hmm, I'm still kind of confused. The reason why I asked is because I saw those different types of coding on the book I'm using and some other examples from a professor. Maybe I just took the wrong way. Here parts of the codes where I saw them.

    Code:
    int main(void)
    {
    char board[3][3] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
    };
    char *pboard = *board; /* A pointer to char */
    Code:
    int x = 10;
    int *p1;
    p1 = &x;
    Code:
    int x = 10; 
    int y = 5; 
    int *ptrx = &x; 
    int *ptry = &y;
    *ptrx = *ptrx + 2 
    ptrx = ptry;
    but in this case, ptrx and ptry are both pointers.

    Thanks
    First of all you should relax if you are beginner. Pointers is a difficult part of C language. You really want some time and patience to understand their function

    Now, you can use printf function in your code in order to print the results ,with this way you will understand better the basic function of pointers.

    ptrx and ptry are pointers yes , *ptrx=*ptrx+2 is pointer arithmetic.

    Code:
    int *ptrx = &x; 
    int *ptry = &y;
    I dont know... My compiler hits when I attempt to declare pointers with this way .

    Code:
     
    
    testtttt.c:6: error: for each function it appears in.)
    or

    Code:
    testtttt.c:14: warning: assignment makes integer from pointer without a cast
    So, I prefer to use this

    Code:
    int x=5; 
    int *ptrx;
    
    ptrx=&x;


    Mr. Lnx
    Last edited by Mr.Lnx; 10-09-2011 at 05:26 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.Lnx View Post
    *ptrx=*ptrx+2 is pointer arithmetic.
    No it's not. It's value arithmetic. You are dereferencing pointers there again, so you aren't doing arithmetic on the pointers, but on the value they contain.
    Quote Originally Posted by Mr.Lnx View Post
    Code:
    int *ptrx = &x; 
    int *ptry = &y;
    I dont know... My compiler hits when I attempt to declare pointers with this way .
    Code:
     
    testtttt.c:6: error: for each function it appears in.)
    or
    Code:
    testtttt.c:14: warning: assignment makes integer from pointer without a cast
    So, I prefer to use this
    Code:
    int x=5; 
    int *ptrx;
    ptrx=&x;
    I think you are getting your errors mixed up somewhere.
    Code:
    int main( void )
    {
        int x = 0, y = 0;
        int *p = &x, *q = &y;
    
        return 0;
    }
    The only warnings I get when I compile that are to let me know that I didn't actually use q or p for anything.


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

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    quzah yes you are right.

    I confused because I answered fast!!!

    *ptrx=*ptrx+2 is NOT pointers arithmetic.

    Pointers arithmetic is this => ptrx=ptrx+2

    *ptrx is a value arithmetic.

    And you are right too for *p=&x;

    I have some time to use pointers in my programs so I have forgotten this declaration... I am using only the basic => *p; => 1st step p=&x => 2nd step

    Mr. Lnx
    Last edited by Mr.Lnx; 10-09-2011 at 05:39 PM.

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    ptrx=ptry sets ptrx pointing to whatever ptry is pointing to so now if you dereference ptrx it will be value of y which is 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM