July 11th, 2003, 10:31 AM
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.
If I'm going about it the wrong way, feel free to verbally slap me in the face.
printf("enter sentence(0 to stop): ");
// dynamic stuff here, I suppose
July 11th, 2003, 11:09 AM
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
this is a single linked list concept
#incude <alloc.h> /* if u dont want fixed arrays */
struct LINK *next; /* this is a pointer concept. points to next structre*/
struct LINK *root;
root = 0;
/* prototypes for functions */
void addstring(char *)
printf("Enter a scentece -999 to stop : ");
void addstring(char *string)
struct LIKN *new, *trace;
root = (struct LINK *)malloc(sizeof(struct LINK));
root->next = 0;
trace = root;
while(trace->next != 0)
trace = trace->next;
new = (struct LINK *)malloc(sizeof(struct LINK));
new->next = 0;
trace->next = new;
struct LINK *trace;
trace = root;
while(trace->next != 0)
trace = trace->next;
Last edited by DJ911; July 16th, 2003 at 02:59 AM.
July 11th, 2003, 11:26 AM
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.
July 11th, 2003, 11:53 AM
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:
You would do the same thing for creating a character array, only the casting would be for char* instead of for int* .
int * CreateIntArray(int size)
int * ip;
ip = (int*)malloc(sizeof(int)*size);
/* invocation */
int_ptr = CreateIntArray(42);
int_ptr = 2003; /* use as an array */
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.
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?
July 11th, 2003, 12:52 PM
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.
July 11th, 2003, 01:47 PM
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.
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.
July 11th, 2003, 03:35 PM
I also answered it, in the other thread, not realising there was a duplicate.
July 12th, 2003, 06:20 AM
this program for u
to understand this Pointer to Pointers concept
char **string, *string1;
memset(string1, '\0', 133);
strcpy(string1, "TTTHHHEEE EEENNNDDD"); /* start point of array */
*string++ = string1;
printf("Enter some strings -999 to end.\n");
string1= (char *)malloc(80);
*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 */
it is ** concept
just experiment ur self on *** concept
July 12th, 2003, 08:17 AM
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.
July 12th, 2003, 10:39 AM
That's a great analogy.
July 12th, 2003, 02:04 PM
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.