newbie question pointers

This is a discussion on newbie question pointers within the C Programming forums, part of the General Programming Boards category; Im very new, what does this mean when a * is in this location char* val; I noticed its used ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Question newbie question pointers

    Im very new, what does this mean when a * is in this location

    char* val;

    I noticed its used here Cprogramming.com Tutorial: Pointers however it is applied to the val and not char

    char *val;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is attached to the variable.
    Code:
    char *ptr, notaptr;
    If it attached to 'char', then both 'ptr' and 'notaptr' would be pointers, but it attaches to 'ptr'. The result is that 'notaptr' is just a char. To make them both pointers, you would need:
    Code:
    char *ptr, *aptr;
    The easy thing to do is to read from right to left:
    Code:
    char *ptr;
    ptr is a...
    * pointer to a...
    char


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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    char* val
    char *val

    are both char pointers, i.e., they point to some char in the heap. The beginning char of a string for instance.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, I misunderstood the question. The whitespace is irrelevant. These are all the same:
    Code:
    char* p;
    char *p;
    char * p;
    char*p;
    Good catch.


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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    so

    char* bla1, bla2, bla3;

    means all the bla1..2..3 are all now pointers?

    or it means just bla1 would be a pointer and bla2..3 are chars?

    ---

    and

    struct val1 * val2;

    means just val2 is a pointer?
    Last edited by starternewb; 03-14-2011 at 10:58 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read my first reply. Only bla1 is a pointer, the other two are not.


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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by starternewb View Post
    so

    char* bla1, bla2, bla3;

    means all the bla1..2..3 are all now pointers?
    No... only the one with the star.

    It is true that whitespace doesn't matter however I find the line you have to be visually deceiving since, gramatically, it seems to say 3 pointers. Which is why I put the * with the variable... for me, it's clearer.

    To make all three ponters you need...
    Code:
    char *bla1, *bla2, *bla3;
    struct val1 * val2;

    means just val2 is a pointer?
    Yes, val2 is a pointer to a struct of type val1.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Thumbs up

    wahoo, thanks everyone for the help much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question : Passing Array Arguments as Pointers
    By NEMEAN in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 03:33 PM
  2. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  3. pointers newbie... -_+
    By ShadeS_07 in forum C Programming
    Replies: 3
    Last Post: 12-25-2008, 05:13 PM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. Question about the introduction to pointers lesson
    By Freemind11 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 11:45 PM

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