Thread: Pointers

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Pointers

    I was just coding a test example and this worked (if it makes any difference, I'm programming in C):
    Code:
    #include <stdio.h>
    
    int num = 2;
    int *ptrnum;
    ptrnum = &num;
    *ptrnum = 3;
    
    int main()
    {
        printf("%d", *ptrnum);
        return (0);
    }
    This worked perfectly and printed to the screen, "3". But then I tried to do this same thing with a string:
    Code:
    #include <stdio.h>
    
    char name[] = "John";
    char *ptrname;
    ptrname = &name;
    *ptrname = "Tom";
    
    int main()
    {
        printf("%s", *ptrname);
        return (0);
    }
    This brought up one error and a warning. The error was that the level of indirection was incorrect (maybe not in that specific wording). What is the difference? I only change data types? How would you do this with strings that I was trying to do? Thanks.

  2. #2
    Unregistered
    Guest
    Strings work differently than any other data type because they are not just a single object, but a collection of chars. Your indirection was incorrect in several areas. The following code works.

    Code:
    #include <stdio.h>
    
    char name[] = "John";
    char *ptrname = name;
    
    int main()
    {
        printf("%s", ptrname);
        return (0);
    }
    My compiler wouldn't let me declare and initialize the pointer variable separately, so I put it together. You also assigned two strings to your pointer variable, that's confusing and doesn't make very much sense in this particular program, that pulled up an error on my compiler as well.
    In your assignment, you aren't assigning the address to the pointer, what you were trying to do was assign the address of the whole array to a pointer of type char, not char[], so that would give an error or undefined output. To assign the array to a char pointer, you do not use the address of operator. Instead you want to just use the array name which is a pointer to the first element so in a sense you are assigning a pointer to an array to a pointer.
    Also in your print all you have to do is call the pointer variable and it will print out the entire array, don't use the indirection operator or you will get an indirection error.

    -Prelude

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just as an aside, here's the working code that does exactly what you were trying to do.

    Code:
    #include <stdio.h>
    
    char name[] = "John";
    char *ptrname = name;
    
    int main()
    {
        ptrname = "Tom";
        printf("%s", ptrname);
        return (0);
    }
    ps. try not to use global variables when possible, they can mux things up and add confusion and chaos to your code ^_^

    -Prelude

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So, you're saying that you can't assign a string to a pointer besides at declaration? And, I read that when you do this:
    Code:
    printf("%d", *ptrPointer);
    The indirection operator signifies to get the object that the pointer points to. So, I figured that I might as well do that for a string.

    And a little summed up (just so I know I'm doing this right), when you delcare a pointer to another data type (besides a string) you have to use the & operator to get the address, right? But you don't do that for a string because it is an array, right? And the indirection operator is only used to get the value of a data type besides a string. You are not supposed to use the * to get the value from a string pointer. Do I have all this straight? Thanks a lot. You saved me a lot of time and sanity. Thanks!

  5. #5
    Unregistered
    Guest
    In the case of an int or the like, the '*' operator will most certainly get the value of the variable being pointed to, just as the '&' operator gets the address. But a string is a pointer to the first element of a chain of chars.

    so if I have the string char name[]="Prelude" then I could access it a few different ways. The first is by element, such as name[1], which would give me 'r'. The second is by pointer, which is just using name to access the array. The variable name is not the array in total, but a pointer to the first element, 'P'. Then that pointer accesses the rest of the array. So to print out an array, or scan in an array, you don't need to use the '*' or '&' operators as they are already being used.

    You've got it exactly right, and explained it better than I did I believe. And it's actually best to think of strings as arrays until you get a feel for how they work when pointers are concerned. It limits confusion.

    An easy way to show how an array is just a pointer, consider this:
    You can access the elements of an array two ways, with the [] operator, or with pointer arithmetic.
    Code:
    printf("%c", name[i]);     //[] operator
    printf("%c", *(name+i)); //pointer arithmetic
    both of those commands do the same thing, they point to the ith element of the name array. So when you use the square brackets, you're using pointers, just not directly

    You're probably wondering why you would want to use pointer arithmetic when the [] operator does the same thing and it's less confusing. I use it because it makes me feel special of course, but there is also a practical use such as when the compiler gives you errors because it doesn't understand exactly what you're trying to do with the array. You use pointer arithmetic to tell the compiler exactly and in no uncertain terms what you want it to do using pointers. When you use [], the compiler will assume what you are trying to do and sometimes it can't do it.

    Sorry, I tend to get carried away, I hope all this helps you at some point ^_^

    -Prelude

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks a lot! You have cleared up more than you can imagine. I find pointers interesting, but very prone to errors. I must just keep working at it. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM