#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    Pointers to pointers


    I can't understand completly the use of pointer to pointer, althought I'm getting closer. I feel that it is something vital to know and thus I'm working hard at learning them. A nice user named d_wise_1 wrote some code in a previous post to try to explain it but it is still too abstract of a concept for me.

    I came out with an idea that will help me (and anyone reading this post) understand pointers to pointer better. I'm writing a simple program that continually ask the user to enter a sentence. If the user enters 0 the program will quit asking the user for sentences. It will dynamically create arrays using pointer to pointer and then print that array to screen. I don't want to use fix arrays. I want the array of sentences to be as big as the amount to sentences the person entered. I'm having a problem though, as I don't know how to do some of these things. If anyone is kind enough to flesh out the function below, it will be greatly appreciated.

    Code:
    int main(){
    
    char str[100];
    
    do{
    printf("enter sentence(0 to stop): ");
    gets(str);
    
    // dynamic stuff here, I suppose
    
    }while(strcmp(str,"0"));
    
    
    return 0;
    }
    If I'm going about it the wrong way, feel free to verbally slap me in the face.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    21
    Rep Power
    0
    if u dont want to create fixed size arrays.....
    Liked list is perfect solution for u
    it is a pretty simple & logical concept, once u understant that then u realize how simple that concept is
    here is a basic & siple example program for Linked list

    Code:
    #include <stdio.h>
    #include <string.h>
    #incude <alloc.h> /* if u dont want fixed arrays */
    
    struct LINK
    {
            char *string;
            struct LINK *next; /* this is a pointer concept. points to next structre*/
    };
    
    struct LINK *root;
    root = 0;
    
    /* prototypes for functions */
    void addstring(char *)
    void display();
    
    void main()
    {
         char *string;
         do{
               printf("Enter a scentece -999 to stop : ");
               gets(string);
               addstring(string);
         }whiler(strcmp(string,"-999")==0); 
         display();
         getch();
    }
    
    void addstring(char *string)
    {
         struct LIKN *new, *trace;
         
         if(root==0)
         {
               root = (struct LINK *)malloc(sizeof(struct LINK));
               strcpy(root->string, string);
               root->next = 0;
         }
         else
         {
              trace = root;
    
              while(trace->next != 0)
                     trace = trace->next;
    
              new = (struct LINK *)malloc(sizeof(struct LINK));
              strcpy(new->string, string);
              new->next = 0;
    
             trace->next = new;
         }
    }
    
    void display()
    {
         struct LINK *trace;
    
         trace = root;
    
         while(trace->next != 0)
         {
               printf("\n%s", trace->string);
               trace = trace->next;
         }
    }
    this is a single linked list concept
    Last edited by DJ911; July 16th, 2003 at 01:59 AM.
  4. #3
  5. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    sorry about that. I know how to use/make linked lists. I want to learn about pointers to pointers. Is there no way of making arrays that are not fixed without using linked list?

    Thanks anyways for the good example on linked list. I'm sure some will come to this post and will find it very useful.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,169
    Rep Power
    2222
    Originally posted by kubicon
    sorry about that. I know how to use/make linked lists. I want to learn about pointers to pointers. Is there no way of making arrays that are not fixed without using linked list?

    Thanks anyways for the good example on linked list. I'm sure some will come to this post and will find it very useful.
    Yes there is, using malloc/calloc and pointers as we've covered already.

    Since I came into C via assembly and Pascal, pointers came naturally to me, but I'm sure that they're a big stumbling block for many. I'm not familiar with on-line tutorials, but the chapter on pointers in the Schaum Outline on Programming in C (I forget the exact title, but it's not the C++ one) covered the subject very well and very thoroughly.

    OK, a pointer and an array name are equivalent; you can treat either one as the other in almost every respect (except for assigning a different address to an array name). So, to dynamically create an integer array of an arbitrary size:
    Code:
    int * CreateIntArray(int size)
    {
        int * ip;
    
        ip = (int*)malloc(sizeof(int)*size);
        return ip;
    }
    
    /* invocation */
    int *int_ptr;
    int_ptr = CreateIntArray(42);
    int_ptr[18] = 2003;  /* use as an array */
    You would do the same thing for creating a character array, only the casting would be for char* instead of for int* .

    And you would do the same thing for creating an array of character pointers, in which case you are now dealing with char** -- think of it as (char*)* , if that syntax would fly.

    So:
    char **str_ary;
    would make str_ary point to an array of character pointers, each element of which is a char* that points to a string. Conceptually, this makes it a two-dimensional array in which each row is a string and each column is a particular character in that string.

    That is how I was using char** . Does that make any sense now?
  8. #5
  9. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    I just finished reading a reply that you answered me about this same topic (sorry that this topic is a repeat, I just wanted to give it some attention by posting it as a new thread) and there both very helpful. I think I'm about 85% close to understanding them 100%. All thanks to you.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,169
    Rep Power
    2222
    Originally posted by kubicon
    After going over and over your "pointer to pointer" example a couple of posting up, I think I'm beggining to understand them.
    thanks oh wise1.

    Regarding this last posting. This
    char *str = "hello";
    is creating a pointer to the beginning of a literal string, right?

    so if I try do do this:
    *str = "hello again";
    Basically what I'm telling the compiler is "Hey bob, overwrite the literal string with this one" in other words "Set the value of what str is pointing to this >hello again", and the compiler says, "but is a literal, I can't do that". I'm I right so far?
    However if I do this
    str = "Hello again";
    I'm just redirecting the str pointer to point to the new literal string. The old literal string will still be there, I just won't be able to access it.

    Am I correct, sensei?
    Hai, honto desu [yes, it is true]. Almost.

    This is nearly nonsensical, which is why it won't (or shouldn't) compile:
    *str = "hello again";

    Literally, it says to set the value of the character pointed to by str to the pointer pointing to the literal string "hello again".

    Or, more hardware-oriented * , take the value of the pointer to "hello again" and store it in the byte location pointed to by str. Because a pointer is four bytes long, you must chop off three of those bytes in order to stuff it into a single byte. And what you end up with is nothing at all like what you originally expected.


    * Footnote:
    I started out as an Air Force computer technician, so I sometimes view software in terms of what it tells the hardware to do. This knowledge of the hardware probably also makes pointers more concrete and real for me and less of an abstraction as they must appear to the purely software-oriented.
  12. #7
  13. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    I also answered it, in the other thread, not realising there was a duplicate.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    21
    Rep Power
    0
    hey kubicon
    this program for u
    to understand this Pointer to Pointers concept

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    
    int main()
    {
    	char **string, *string1;
    
    	string1=(char *)malloc(133);
    	memset(string1, '\0', 133);
    	strcpy(string1, "TTTHHHEEE EEENNNDDD");         /* start point of array */
    	*string++ = string1;
    
    	printf("Enter some strings -999 to end.\n");
    
    	do{
    		string1= (char *)malloc(80);
    		memset(string1,'\0',80);
    		gets(string1);
    		*string++ = string1;
    	}while(strcmp(string1,"-999"));                 /* end point of array */
    
    	while(strcmp(*--string,"TTTHHHEEE EEENNNDDD")); /* travel upto starting point */
    
    	while(strcmp(*++string,"-999"))                 /* travel upto ending point */
                    	printf("%s\n", *string);
    	getch();
    	return 0;
    }
    Ok
    it is ** concept
    just experiment ur self on *** concept
  16. #9
  17. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    i read an analogy somewhere of pointers to pointers that worked well for me:

    the (hackneyed) library example

    you go into a library (b.c. (before computers)) and you're after a book titled 'whatever'. so you go to where the card index system is, locate the title index, as opposed to the author index. its in alphabetical order so you find W easily then flick through until whate... and find the card. <<< all that part amounts to, in the analogy, pointer no.1 - locating the necessary card that points to the book you're looking for.

    then using the information on the card you can easily locate the actual book itself on the shelves. <<< that card that locates the specific book represents the second pointer. and the book itself obviously represents the data.


    the card index system that you look through to find your book title is nicely ordered alphabetically to make it easy for you to find. the books on the shelves could be jumbled up in any order what so ever - maybe the chronological order that they were aquired by the library, but you can still find them by title alphabetically easily. it would be a lot of extra work on the library's part to keep the books physically ordered alphabetically. also, even if they could spare the effort to order the books by title alphabetically on the shelves, (which can be seen as an analogy of using a single pointer) how would you do an author alphabetical index at the same time?

    so what this card index system allows is for the books to viewed in any chosen/designed order, without physically moving loads of heavy books round. you can change the order of the cards very easily and quickly - you've just got to shift little bits of card round. you can make up a new order - the book's published dates maybe, just by making a new index box with new bits of card.
  18. #10
  19. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    That's a great analogy.

    Thanks
  20. #11
  21. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    ah i'm wrong. the library index card thing can be done with a single list of pointers i've just realised - so not pointers to pointers at all. you can rearrange a single list of pointers in whatever manor you want and get the equivelent of the library card index system. so forget that, at least as far as pointers to pointers go.

IMN logo majestic logo threadwatch logo seochat tools logo