question about pointers without the *

This is a discussion on question about pointers without the * within the C Programming forums, part of the General Programming Boards category; when I declare a pointer, and refer to it without the *, I think I am using it as a ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    question about pointers without the *

    when I declare a pointer, and refer to it without the *, I think I am using it as a normal variable (since I know pointers hold addresses in memory, and the * operator will retrieve the value at the address stored in the pointer). is this correct? for example:

    Code:
    int i, *p;
    
    i = 1;
    p = &i; /* this line, I am assigning the address of i to the variable p, correct? */
    what is "p" at the point of the commented line (without the *)? is it simply a variable at this point?

    forget about strings being used without their index for a second, I DO understand that this will generate the address of the first element of the string. but just that fact is probably what is leading me to this uncertainty. but also the fact that this code is legal, makes me think my answer to my above question is correct:

    Code:
    char str[80] = "somestr", *p;
    
    p = str; /* the equivalent of using &, the "address of" operator, which is unnecessary since the compiler will automatically substitute str with the address of its first element */
    p would be a variable declared as a pointer to char in the above, and str would be a constant address of the first element in the string.

    would anyone please just let me know if I have this all correct here?


    thank you in advance!
    Last edited by Bleech; 10-24-2006 at 02:14 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    A pointer is a variable that holds the address of something. If you dereference the pointer (using *) you are dealing with the "thing" it points at, rather than the pointer itself.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    exactly. if you dereference it with *, you end up at the address that it contains (hence the reason why they say do not dereference a pointer that does not contain a valid address, you will be attempting to obtain memory at an undefined location).

    I'm so happy now that I think I have finally understood pointers 100%. they are the only thing that I was shaky with in C, and probably the most complex and powerful part of the language (and now that I seem to understand them, they don't seem all that complex). soon I'll be able to move on to C++, but not just yet, I am happy with C for the moment and want to build more experience.

    user input is still appreciated however, I shouldn't get too excited, I might still be wrong on some of what I said...
    Last edited by Bleech; 10-24-2006 at 02:40 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Um -- you could always compile it to see if you got it right, instead of coming all the way here...

    Code:
    int main(void)
    {
        char str[] = "Hello, World", *p;
        int i = 12345, *q;
        printf("The address of %s is %x\n", str, str);
        p = str;
        printf("p has been assigned the value %x, it now points to %s\n", p, p);
        q = &i;
        printf("The address of i is %x; it holds the value %d\n", q, *q);
        return 0;
    }
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and probably the most complex and powerful part of the language
    Powerful, sure. Complex, not really. Streams are more complicated in concept and in practice than pointers. It's the programmers who don't fully understand the concept of indirection that make pointers complex. In other words, it's all in your head.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 05:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

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