0

Please forgive me if the title is not 100% correct. I have a function which takes two inputs - a pointer to a string, and a pointer to a pointer to an array (?). The function I am writing is
int string_parser(char *inp, char **array_of_words[])
What I want to be doing is taking these two arguments and the function should return

  1. the amount of words in the string array (string array is char *inp)
  2. a pointer to an array of pointers char **array_of_words[] - each element in the array pointing to the address of the first character in each word (I apologise if that is wordy)
    I have created the pointer to an array of pointers, and allocated space to this array
    char **startOfWords_ptr = (char **) malloc(amountOfWords * sizeof(char*));
    and have been manipulating the contents just fine. I now want to pass the array at *start_of_words back to array_of_words - but I don't understand how to do it

With *array_of_words = *(startOfWords_ptr); am I saying: a pointer to point at the starting address of the array of pointers?
Surely I am not because I don't get the same values when printing...

printf("%p\n", (void *) *array_of_words); // 0x401f2c, o
printf("%p\n", (void *) *startOfWords_ptr); // 0x401f2c, o

printf("%p\n", (void *) array_of_words[1]); // 0x401f2c, o
printf("%p\n", (void *) startOfWords_ptr[1]); // 0x401f30, t

Suggestions with comments as output

*array_of_words =  (startOfWords_ptr); // This currently does not work

printf("%p, %c\n", (void *) array_of_words[0], *array_of_words[0]); // 0xcbbdd0, ;
printf("%p, %c\n", (void *) startOfWords_ptr[0], *startOfWords_ptr[0]); // 0x401f3b, o

printf("%p, %c\n", (void *) array_of_words[1], *array_of_words[1]); // 0x401f2c, o
printf("%p, %c\n", (void *) startOfWords_ptr[1], *startOfWords_ptr[1]); // 0x401f3f, t
17
  • 1
    In C++ one would normally use std::string and std::vector and avoid raw arrays and memory allocation. So no, not the same when using natural constructs of the language. Commented Mar 24, 2017 at 20:35
  • c++ tag removed Commented Mar 24, 2017 at 20:37
  • 2
    do you want *array_of_words = startOfWords_ptr; ? Commented Mar 24, 2017 at 20:49
  • 1
    It looks like you are looking for *array_of_words = startOfWords_ptr;. Commented Mar 24, 2017 at 20:53
  • 1
    @fOrceez: Let me make sure I understand your requirement - given an input string like "This is a test" (inp), you want to create an array of pointers to the first letter of each word? Commented Mar 24, 2017 at 21:35

2 Answers 2

1

Okay, so let's look at this from the perspective of the caller of this function:

int main( void )
{
  char my_input[] = "This is a test";
  char **my_output = NULL;

  int count = string_parser( my_input, &my_output );
  for ( int i = 0; i < count; i++ )
    printf( "pointer #%d: %p\n", i, (void *) my_output[i] );
}

We know that after calling string_parser, my_output will point to the first of a sequence of pointers to char. Since we need to modify the value of my_output, we must pass a pointer to it in the call (&my_output).

This means that the prototype for string_parser needs to be

int string_parser( const char *inp, char ***array_of_words ) 
{
   ...
}

In the context of a function parameter declaration, T a[N] and T a[] are both treated as T *a, so char ***array_of_words is the same as char **array_of_words[] - both are ultimately char ***.

So given

I have created the pointer to an array of pointers, and allocated space to this array char **startOfWords_ptr = (char **) malloc(amountOfWords * sizeof(char*)); and have been manipulating the contents just fine. I now want to pass the array at *start_of_words back to array_of_words - but I don't understand how to do it

you have it backwards - you'd assign the startOfWords_ptr to *array_of_words:

*array_of_words = startOfWords_ptr; // char ** = char **

array_of_words has one more level of indirection than startOfWords_ptr, so we need to dereference it to get the types to match. After this assignment,

*array_of_words[i] == startOfWords_ptr[i] // char * == char *
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, helped me get an understanding of what I needed. Turns out even when I had the correct assignment, I was printing incorrectly. Dereferencing when I didn't need to, not dereferencing when I needed to. Answer has been accepted :)
1

this parameter in the function signature:

char **array_of_words[]

can be re-written as:

char ***array_of_words

I.E. either write it as:

char **array_of_words

or as:

char *array_of_words[]

This is very similar to the different ways the second parameter to main() can be written.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.