#Need help iterating through a char ** pointer array

9 messages · Page 1 of 1 (latest)

solar axle
#

I have a homework assignment where I have to create a function called add_string(char **existing, const char *add) which also returns a **char type. The goal is to basically make a new array that copies all of the existing array of strings and appends the add string on the end as well.

I am having some difficulty figuring out how to actually iterate through the **existing array that has been passed through.

I have tried inputing a for loop and looping that way. I tried just outputting existing, with printf("%c", *existing[0]); And my most recent attemp has been this:

char *ptr = *existing;
       
    while(ptr != NULL)
    {
        printf("test");
        fflush(stdout);
        ptr++;
    } 

Currently my output is this:
timeout: the monitored command dumped core

Could anybody help me figure out how to correctly iterate through this array of strings?

dapper tundraBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ruby kelp
#

that would depend on what exectly exsiting is, and how it was constructed. pointers don't hold any info about the length of the 'thing' they point to. i would assume existing hold it somewhere(?)

usually thats done by setting the last char * to NULL

#

if thats indeed the case - one can do:

for(; *exisitng; exisiting++) {
  puts(*exisiting);
}```
#

otherwise - you'd need to know how many 'strings' exsiting points to

solar axle
solar axle
ruby kelp
#

in the exact same way, replacing puts with printf("%s\n", ...) i guess. what wrong with puts exactly?