Hey, how do you declare an array of strings using double pointer ?

I normally use

char* str[] = {"this", "works", "fine" ,"for", "me"};

But I want to know if there is a way of declaring array of strings using something like

char** str;

Please show me how to put values in it also, like in the example I stated above.

Edited 4 Years Ago by tubby123: n/a

No! >.<

Show what you have tried. :D

@Adak - hmm, I though about it, and feel that there is no real need of it.

The only place where you could use it however is, if you write something like

char* str[] = {"thank","you","adak"};
char** s[] = {str+1, str+2, str+3};

printf("%s",**(s+2));

if you want to print adak.

Thanks, let me know what you think of it.

I believe you might be immensely helped by taking your simple code:

char* str[] = {"thank","you","adak"};
char** s[] = {str+1, str+2, str+3};

/* now change "adak" to "Alex", right HERE
   and print Alex, instead of adak
*/


printf("%s",**(s+2));

;) Now tell me what you think!

Edited 4 Years Ago by Adak: n/a

I believe you might be immensely helped by taking your simple code:

char* str[] = {"thank","you","adak"};
char** s[] = {str+1, str+2, str+3};

/* now change "adak" to "Alex", right HERE
   and print Alex, instead of adak
*/


printf("%s",**(s+2));

;) Now tell me what you think!

hmm,

#include<stdio.h>

char* str[] = {"thank","you","adak"};
char** s[] = {str,str+1,str+2,str+3};

int main(void)
{
 **(s+2) = "Alex";
 printf("[%s]\n",**(s+2));
 
 return 0;
}

Does that answer the question ?

Ok, I have 3 doubts, and please answer using serial numbers. adak, I feel I am going to get this clear with a couple of replies from your side, so let's get started.

1)I still don't get why people use **.
I mean, I thought it is for array of strings, but that can be done by this too.

#include<stdio.h>
#include<string.h>
#include<malloc.h>

int main(void)
{
 char* str[5];
 char* ptr;
 int i = 0;
 
 for(i=0 ; i<5 ;i++)
  str[i] = (char*)malloc(200 * sizeof(char));

 ptr = (char*)malloc(200 * sizeof(char));
 
 strcpy(ptr,"This is a test by tubby and it works fine.");
 
 str[0] = ptr;
 
 printf("[%s]",str[0]);
 
 return 0;
}

2)Doing it the 2-pointer way - Why would you want to store the address of the pointer to char** str; and then print the string using the address in *str[0]. Ok, I tried writing code for it using 2 pointers.This is what I came up with. But it doesn't work.

#include<stdio.h>
#include<string.h>
#include<malloc.h>

int main(void)
{
 char** str;
 char* ptr;
 int i = 0;

 ptr = (char*)malloc(200 * sizeof(char));
 
 strcpy(ptr,"This doesn't work. Is it because I haven't allocated memory for str. I don't know how to.");
 str[0] = &ptr;

 printf("%s",*str[0]);
  
 return 0;
}

3)AND THE MOST FUNDAMENTAL DOUBT, WHICH IS WHY , I THINK , I STILL HAVEN'T GOT THIS.
CASE1
When we say

char* ptr;
malloc memory for ptr.
strcpy(ptr,"This is a test");

what does ptr contain ? I think ptr is a pointer to a character, so it holds the address of 'T'.
Urghhh, I don't get this, let me visualize the memory as
OxFF00 OxFF01 OxFF02 OxFF03 OxFF04 OxFF05
---------------------------------------------
I I I I I I I
---------------------------------------------
Let's assume ptr = 0xFF00, so what does that location contain ? Does it contain 'T'. (I mean, T from "This is a test")

So is it safe to say that this is how it looks in the memory ?
OxFF00 OxFF01 OxFF02 OxFF03 OxFF04 OxFF05
-------------------------------------------------------
I T I h I i I s I I i I s I I a I I t I e I s I t I
-------------------------------------------------------

This sounds logical, then, why do people say ,
pointer holds the address of a character, but from what I have written, ptr holds 'T'.
Sir, I sound like a maniac, but the thing is , I really want to understand this, and I feel that I'm so close to getting it.

CASE2
Whatever I have written in CASE1 is nonsense, and this is how it works.
When I write,

strcpy(ptr,"This is a test");

, the compiler already makes space somewhere in memory for each of these characters - 'T','h','i','s',' ','i','s',' ','a','t','e','s',t'.
Each of these characters is stored in a different location in memory, and ptr actually holds the address which holds 'T'. In that case, it is safe to say what people preach everywhere, that a pointer actually holds address.

4)Which is correct? 3-CASEA or 3-CASEB ?

Thanks a lot. :) :p
Will be back after you reply.

Hello

Here is my little help:

include<stdio.h>

include<string.h>

include<malloc.h>

int main(void)
{
char** str;
char* ptr;
int i = 0;

    /*
    You need to allocate memmory for you **char **str** variable        
    A pointer is an integer of 2 or 4 bytes, and its content is the memmory address of something of your choise.

    The same way you allocated memmory for **char *ptr**, you have to allocate for the variable str.        
    How?        
    You have to call malloc() and get the size of a pointer-to-char (char *)
    and then cast the return value to a pointer-to-char-pointer (char **)               
    Please correct me if I am wrong.
    */


    str = (char **)malloc(sizeof(char *));         
    ptr = (char*)malloc(200 * sizeof(char));

    strcpy(ptr,"This doesn't work. Is it because I haven't allocated memory for str. I don't know how to.");
    /*
    str[0] = &ptr;
    By doing this, you are telling that you want the char * at 0 of your array of char *, to point 
    to the address of a pointer-to-char, and it is wrong.        
    str is a pointer-to-char-pointer, and it can store the address of a pointer-to-char (char *).        
    str[0] is dereferencing str and get the value at the index 0.
    *(str + 0) = *str = str[0]
    *(str + 1) = str[1]
    *(str + 2) = str[2]
    *(str + n) = str[n]         
    str[0] must receive a char *, so the correct asignation will be        
    str[0] = ptr;
    or
    *str = ptr
    or
    str = &ptr;        
    I think they are the same.        
    Please correct me if I am wrong.        
    */
    str = &ptr; 

    printf("[%s]\n", ptr);
    printf("(%s)\n", str[0]);

    return 0;

}

This article has been dead for over six months. Start a new discussion instead.