Pointer Mock Exam Question

This is a discussion on Pointer Mock Exam Question within the C Programming forums, part of the General Programming Boards category; Originally Posted by spadez isnt *p defining p as a pointer. Not quite. Consider the syntax for defining a pointer ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by spadez
    isnt *p defining p as a pointer.
    Not quite. Consider the syntax for defining a pointer and initialising it to be a null pointer:
    Code:
    int *p = NULL;
    Notice that the type name int is part of the syntax. Compare it to this dereferencing of p to print what the pointer points to:
    Code:
    printf("%d\n", *p);
    Now, the type name of the pointed to type is not part of the syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by spadez View Post
    I find this slightly confusing, isnt *p defining p as a pointer. In which case how is *p "dereferencing the pointer". I might need this dumbing down a little :s
    Another reason I dislike T * syntax...
    You can also define pointers as:
    T* p;
    Which might make it clearer.
    Even clearer is:
    T* p1;
    T* p2;
    Instead of:
    T *p1, *p2;

    p2 belongs to the same line as the declaration, hence it is also a declaration. Merely putting "*p" somewhere does not mean you are declaring a new variable since there's no type before it. Instead it's dereferencing the pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 12:47 PM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 02:53 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 04:44 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 10:51 AM

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