3

I was practicing string arrays but it seems like i don't undestand how this works, can someone explain to me what would be a correct way to formulate this code?

AIM OF THE CODE: Assign 2 strings to an array of strings and print them by using a function.

ERRORS: The compiler doesn't find any error, but i simply don't get any output in the terminal.

#include <stdio.h>

#define MAX 100

void function(char **);

int main()
{    
    char *a[MAX]; /*array of max 100 strings*/
    a[0] = "test0";
    function(&a[MAX]);    
    return 0;
}

void function(char *a[MAX])
{    
    a[1] = "test1";
    printf("%s",*a[1]);
    printf("%s",*a[0]);
}
6
  • Do you mean to use "%d" in the first print? What's going wrong? Commented Dec 9, 2021 at 15:37
  • No, it was a minor mistake but isn't the main problem, i updated the question Commented Dec 9, 2021 at 15:38
  • What is your specific problem? -- You pass the pointer right after the last element of your pointer array to the function. I'm sure this is not what you want. Commented Dec 9, 2021 at 15:46
  • 2
    You are passing the function the address of the one after the last element of a, which is also a wrong type. You should be getting a bunch of warnings here. Commented Dec 9, 2021 at 15:46
  • Sorry if the question wasn't clear enough, my purpose was to print the value in a[1] and a[0], but i'm just getting a few errors, i'm going to edit the question adding those too. Commented Dec 9, 2021 at 15:49

3 Answers 3

1

The compiler doesn't find any error, but i simply don't get any output in the terminal.

That's because you're passing the address of something that's not what your function expects.

function(&a[MAX]);

You're passing the address of the item at index MAX in array a, but your function interprets that as the address of the whole array. You should instead write:

function(a)

It seems like you've confused the way a parameter is declared with how it's used in a function call. When you declare a function, you have to specify the types of the parameters, so you say e.g. char *s[] to indicate an array of pointers to characters. But when you call a function, the compiler already knows what type the function expects, so you only need to pass a variable that matches the declared type. The square brackets in this context, like a[MAX], are interpreted as subscript that selects one element of the array.

Aside: As a matter of style, function is a terrible name for a function. I know this is just a tiny test program, so no big deal, but try to get in the habit of giving things descriptive names.

Also:

printf("%s",*a[1]);
printf("%s",*a[0]);

Here you're accessing the item at index 1, but you haven't yet stored anything there. That's not a good plan. Either remove that first line, or change your code to store something in index 1.

Also, you don't need to dereference the elements. Each element in the array is an array of characters, and printf() will expect a value of type char *, which is what each a[n] will be. So just use:

printf("%s", a[1]);
printf("%s", a[0]);
Sign up to request clarification or add additional context in comments.

4 Comments

Does referring to the array as function(a) , give to the function the whole array of arrays? p.s. i did assign a[1] but i tried doing it inside the function to test how it worked.
Sorry, missed that about a[1]. Anyway, yes, referring to the array as a provides the value of the array, which is really just the address of the first element (the one at index 0). For the same reason, your printf() statements should refer to the strings as e.g. a[0], not *a[0]. *a[0] is the first character of the first string in the array. Note my edit of a moment ago adding that point.
Thanks that seems to work! What if i want to access the 3rd letter of the string a[0] inside the function?
There are shorter ways, but best to keep things simple while you're learning: char *s = a[0]; char c = s[2];
1

You seem to have some confusion with the pointers you're working with and their correct types. Hopefully the program below can clear some things up. Explanations are in the comments:

#include <stdio.h>

#define MAX 100

void function(char* (*aPtr)[MAX])
{
    // Since aPtr is a pointer, we need to derefecence it with * before assigning
    // string literals
    (*aPtr)[1] = "test1";
    printf("In function\n");
    printf("%s\n",(*aPtr)[0]);
    printf("%s\n\n",(*aPtr)[1]);
}

void function2(char** a)
{
    // Remember, * and [] can both be used to dereference in C
    // This is equivalent to *(a+2) = "test2";
    a[2] = "test2";
    printf("In function2\n");
    printf("%s\n",a[0]);
    printf("%s\n",a[1]);
    printf("%s\n\n",a[2]);
}

int main(void)
{
    // This is an array of MAX char pointers. Just on declaration, each pointer in the array
    // points to nothing. You can point them to a string literal (as you have done), or use
    // something like malloc to allocate space for each one
    char *a[MAX];
    // This is a pointer to an array of MAX char pointers. This is the type you're trying to
    // pass to `function`, which is not used correctly
    char* (*aPtr)[MAX] = &a;
    // I've included these prints so you can see the difference between `a` and `aPtr` via pointer
    // arithmetic. In this printf, `a` "decays" to a pointer to the first element in the array.
    // The first element of the array is a `char*`, so a pointer to that is a `char**`, so
    // that's what `a` is in this context, pointing to a[0]. What the actual address is here isn't
    // important, just think of this as offset 0
    printf("pointer to first element      = %p\n", (void*)a);
    // This is the explicit address of the first element in the array. Notice how it is
    // identical to the address of the array above.
    printf("address of first element      = %p\n", ((void*)&(a[0])));
    // This illustrates the "decay" concept again. `a` decays to a `char**`, and +1 on that
    // shows pointer arithmetic. On this architecture, pointers are 8 bytes, so a+1
    // advances the pointer by 8 bytes, which points to the 2nd element in the array.
    // This output will be the offset +8
    printf("pointer to second element     = %p\n", (void*)(a+1));
    // This shows the address of aPtr. Remember, this is a pointer to an array of char
    // pointers 100 large. This base address is also the address of where the array starts,
    // so it will be identical to offset
    printf("address of array              = %p\n", (void*)aPtr);
    // This is where the different pointer types are illustrated. Since char pointers are 8
    // bytes on this architecture, an array of 100 of them is 8*100 = 800 bytes. So aPtr+1
    // here performs pointer arithmetic on that type, meaning it advances the pointer 800
    // bytes. The print out here will be offset +800.
    printf("pointer to next array element = %p\n\n", (void*)(aPtr+1));

    // assign a[0] to point to the string literal "test0"
    a[0] = "test0";

    // This is what you're trying to do (but doing incorrectly) with your function call.
    // This passes the address of `a`, a pointer to an array of char pointers 100 large.
    // This is identical to passing `&a`, the address of `a`.
    function(aPtr);
    // `a` in this context "decays" to a pointer to its first element. Its first element is
    // `char*`, so here `a` is `char**`. And this is the signature you'll find in function2
    function2(a);

    // print out in main to show all the assignments were made
    printf("in main\n");
    printf("%s\n",a[0]);
    printf("%s\n",a[1]);
    printf("%s\n",a[2]);

    return 0;
}

Demo

2 Comments

Wow! What an elaborate answer, thank you! I still have a doubt, what if in function 2 i want the 3rd letter of a[0]?
@Pinguiz I always need to think about pointers to arrays, I spelled this out just as much for me as you heh. Third letter of first string is a[0][2]. In function2, a is a char**, so a[0] is the first char*, and a[0][2] is the the 3rd char offset from the first char*. You can go to the Demo link and play in that sandbox, don't forget "%c" is the format specifier for printing a char.
-1

You need to pass the pointer of the array i.e.

function(a)

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.