Using pointers - asterisks needed ?

This is a discussion on Using pointers - asterisks needed ? within the C Programming forums, part of the General Programming Boards category; Hi, WHen i finished declaring a pointer, for example : Code: char *strPtr; Do i have to place an asterisk ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Using pointers - asterisks needed ?

    Hi,

    WHen i finished declaring a pointer, for example :
    Code:
    char *strPtr;
    Do i have to place an asterisk before the variable whenever i use it? But i saw in some programs that '*' is not needed ?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    it completely depends on what you intend to do...

    In most cases, using the asterik in the program points to the MEMORY ADDRESS of that variable, and withotu an asterik it points to what the memory address contains.

    There are exceptions and I really can't remember.. pointer rules suck.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Should be the other way around, if strPtr is a properly initialized pointer, 'strPtr' will contain some memory address, '*strPtr' dereferences the pointer making its value whatever value is at that address.

    Pointer rules don't suck, they just take time to fully understand

  4. #4
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    what helps me to understand pointers is

    when you declare a ptr (note the lack of a '*' cos i'm talkin about it)

    char str[10];
    char *ptr; /* iv'e just declared a pointer & '*' is the address of it*/

    ptr = str; /*when you assign a var to it-no need for the '*' cos your telling the compiler to attach the address of the first element in str to ptr */

    if you want your compiler to look at where the pointer is pointing to, then that's what you state

    while(*ptr) (for while at the address of ptr)
    or if(*ptr == 'A') (if at the address of pointer equals letter'A')

    what you need to remember is that a ptr will only access a single element within an array.

    hope it helps geez
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by *ClownPimp*
    Should be the other way around, if strPtr is a properly initialized pointer, 'strPtr' will contain some memory address, '*strPtr' dereferences the pointer making its value whatever value is at that address.

    Pointer rules don't suck, they just take time to fully understand
    that's why they suck.. I can never remember them..

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You only need the * if you must access a single character. To refer to the character array, just use strPtr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Variables | Pointers in arguments really needed?
    By bobbelPoP in forum C++ Programming
    Replies: 40
    Last Post: 08-04-2008, 09:45 AM
  3. help needed with void pointers.
    By broli86 in forum C Programming
    Replies: 14
    Last Post: 07-01-2008, 12:00 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Array of pointers
    By falconetti in forum C Programming
    Replies: 5
    Last Post: 01-11-2002, 06:26 PM

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