pointers...

This is a discussion on pointers... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int main() { char array[] = "test this"; char next[] = "newline"; char *ptr = ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    pointers...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char array[] = "test this";
    	char next[] = "newline";
    	char *ptr = array;
    	char **ptr2;
    	ptr2 = &ptr;
    
    	printf("%s\n%s\n", array,ptr);
    	ptr2 = (char**)next;
    	printf("%s\n", ptr2);
    	printf("%s\n%s\n", array, ptr);
    	return 1;
    }
    Why is it that when I modify **ptr2 it doesnt modify array and ptr as well? Im also not sure why I couldnt just do ptr2 = next; but the compiler wouldnt accept anything but that.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> Im also not sure why I couldnt just do ptr2 = next
    Because they're of different types. ptr2 is a char**, whereas next is a char[] (which is char* in disguise).

    There's a new FAQ entry for pointers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I knew most everything in that FAQ and it really didnt start to solve my problem unless im missing someting.

    I did do a little more thinking however and Im guessing when I set str2 = (char**)next; str2 no longer points to &str, it now points to the array next.

    If that is right my next question would be how can I set my ** pointer to another pointer and modify that pointer in a way that it will effect all three variables. I know I can do it by **ptr2 = 'c'; or some other character but Im wondering how I can change ptr2 to a whole different string. Im pretty positive I would have to change **ptr2 in order to do what I want to, but I once again could be wrong.

    By the way this is just so I can learn something about pointers to a pointer...this code has no useful purpose at all(incase everyone thinks im an idiot).

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well not sure quite what u want but my guess is this...

    keep the char** pointing at the char*
    make all accesses thru the char** selecting the array by modifying whichever array the char* points to....
    Code:
    char array1[]={"dogs"};
    char array2[]={"bollocks"};
    char* parray = array1;
    char** pparray=&parray;
    **pparray='b';
    parray = array2;
    **pparray='d';
    //now print the arrays.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Im actually trying to find a way to change **ptr2 to a string rather than a character. Something like **ptr2 = "this new string"; but that doesnt work in the compiler so Im looking for an alternative.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use strcpy() but be careful. those arrays are only so big remember....

    strcpy(*pparray,"new");
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Stoned_Coder
    use strcpy() but be careful. those arrays are only so big remember....

    strcpy(*pparray,"new");
    Which is why you should be using strncpy instead. It is usually (always?) better to use a function that provides boundry checks than one that doesn't. I'd say always, but I'm sure some one will come up with some bizzare, ass backwards way of using one to find an exception to the "always" clause.

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

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i know i was just too bloody lazy to mention it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Well that works. I have a couple questions though. I did
    strcpy(*ptr2, "new"); and now ptr and array read "new". However when I print out ptr2 its some jiberish...its now "new". Why is that?


    Also if ptr2 is **ptr2 why strcpy(*ptr, "new"); ?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    type **ptr2ptr
    type *ptr
    type instance;
    
    ptr = & instance;
    ptr2ptr = &ptr;
    You dereference a pointer to get what it points at. Thus:

    *ptr <-- This produces the value stored in 'instance'.
    *ptr2ptr <-- Produces the value stored in 'ptr'.

    **ptr2ptr <-- Produces the value stored in 'instance'.

    Thus, if you want to use a pointer to a pointer as you would the pointer it points to, you add one level of indirection. This treats it like the point it points to. To further dereference that, you add one more.

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

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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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