Search code examples
cpointersdouble-pointer

How to pass the address of a double pointer to another double pointer


How can I pass the address of a double pointer to an other? I have this code and it's working correctly only if i set the commented line. Why the size is different?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char *s1[]={"this","is","a","test"};
    char **s2;
    int i;
    s2=malloc(sizeof(s1)*sizeof(char *));
    s2=s1;
    for(i=0;i<sizeof(s2)/sizeof(char *);i++)//for(i=0;i<sizeof(s1)/sizeof(char *);i++)
        printf("%s",*(s2+i));
    return 0;
}

Solution

  • The commented line uses sizeof(char*[4]), which presumably is four times the size of sizeof(char**) on the uncommented line.