Home > Software design >  Using Arrays of strings in function
Using Arrays of strings in function

Time:12-10

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]);
}

CodePudding user response:

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]);

CodePudding user response:

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

CodePudding user response:

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

function(a)
  • Related