Sunday, July 12, 2009

Using C how can i create a pointer to a pointer.?

I have a hard time to understand char **abc. Can somebody pls explain this. Tell me how this relate to a string. It is said that abc can be considered as an array. I don't have any idea about how this happens. I'm so confused. Pls explain. I knw some C.

Using C how can i create a pointer to a pointer.?
if you declare a string like





char *myString = "asd";





then myString a pointer to a character. It contains the address of the memory location which contains the character 'a'. Subsequent memory locations (i.e Mystring+1, myString+2) contain the characters 's' and 'd'. Make sure you understand this much before proceeding.





Now, a declaration like


char **abc;


creates a pointer abc that points to (or contains the address of) a character pointer like myString above.





Pointers to pointers are like a treasure hunt games where the first clue tells you where the second clue is; and the second clue tells you where the treasure (data) is.





:) Happy huting !!
Reply:Well first of all I don't think you would normally need to create a pointer to a string which would be a pointer to a pointer.


However it this is what you would like to do then lets simply create a string first:


char s1[] = "Able";





In the instance above s1 is a simple pointer. Assuming that the string s1 is located at memory address 1000 (for illustration purposes) the actual value of s1 would be 1000 demonstrated by:


printf("%d", s1);


However the string pointer s1 is stored elsewhere, say memory address 800. The breakdown of the structure would be :


s1[0] --%26gt; Memory location 1000 --%26gt; A


s1[1] --%26gt; Memory location 1001 --%26gt; b


s1[2] --%26gt; Memory location 1002 --%26gt; l


s1[3] --%26gt; Memory location 1003 --%26gt; e





Now to create a pointer to the you would code:


char *p_s1 = %26amp;s1


Lets assume p_s1 is stored at memory location 2000.


printf("%d", p_s1); would then output 800 the address of the pointer s1, not the address of the string.


Now if you wanted to output the value of the string you would do this syntax **p_s1:





Lets break this down to see what happens


p_s1 gives 800 the address of our string pointer


*p_s1 gives 1000 the dereferenced value of our string pointer


%26amp;p_s1 gives 2000 the memory address of p_s1 (not needed but hre to give reference)





so then:


*p_s1 is equivilent to 1000 (what s1 points too) and


**p_s1 is equivilent to 64 (the character A) the dereferenced value of 1000 or s1





Note the following is NOT a pointer to a pointer but gives an illustration of how pointers and string are commonly used.


This is the code of the ANSI C strlen() function:





int strlen(char *string)


{


char *ptr = string; /* ptr is used to reference each char in the string variable*/


while (*ptr != '\0') ptr++;


return ptr - string;


}





This might be more of what you want to do instead of a pointer to a pointer. Hope this helps
Reply:"char **abc" means "char* abc[]" - or - "abc is an array of pointers to char - we don't know how many elements long".





...and now the windy version...


In C, you don't really have strings at all. The compiler does not know anything about strings.


All you have is arrays of characters.


What makes an array of characters behave as a string are the standard C library functions, which expect the end of the string to always be marked with a "nul" character, 0x00.


Moving on to arrays...


When you declare an array you tell the compiler the name or symbol you want it to have, the type of data it will contain, and the number of elements. e.g.


int A[5];


Now the compiler initially uses this information to allocate space for the array, in this case 5 * sizeof(int) bytes.


Everywhere it is referenced, however, the name A behaves exactly the same as (const int *).


For example if we have:


int A[5];


int *p;


We can say p = A; but we can't say A = p;


We can say A[3], and we can say p[3]. No difference.


Furthermore we can do pointer arithmetic with p because we told the compiler what type of critter p can point to --


*(p+3) = 1;


AND you can do pointer arithmetic on A (as an rvalue)...


*(A+3) = 1; -- same thing, see? A+3 == %26amp;(A[3])


Now some tricky stuff. If you think about it, p is an array you can move. It is like int p[] - where you know how big each element is, but not how many elements there are.


Let's assign:


p = A+3; ...so p points to A[3] ... p = %26amp;(A[3])


... p[0] is the same variable as A[3]
Reply:look this sample program below


as you are aware *ch is used to denote a string or is a pointer to a string


** is a pinter to an array of strings





#include %26lt;stdio.h%26gt;


int main()


{char *my,**mc, *names[]={"anil","antony","afsal"};


my="Sam";





mc=names;


printf("%s\n",my);printf("%s\n",*mc++)...


printf("%s\n",*mc++);


printf("%s\n",*mc);


printf("Hello world\n");


return (0);


}





my is a string array


%26gt;mc=names;


**mc is pointer to an array of strings the *names[]


%26gt;*mc++


*mc++ will pint to the next element on the string array *names





hope it is clear now

online survey

No comments:

Post a Comment