2

I think to pass my array in a wrong way by reference cause I have lot of warning for its. Could you help me?

void func (char **array[], char b){
*array[0]=&b;
}

int main () {

char *array_in_main[SIZE];
char b_in_main='b';
func (*array_in_main, b_in_main);
return 0;

}

Yes, I know. It's a no-sense example but my doubt is on syntax. Check it please!

ilacollie
  • 57
  • 5

4 Answers4

5

You have a view problems here.

First you passing the wrong type to your function. If you want to modify an array in a function you do not have to pass it with a pointer to the array since the array decays to a pointer to its first element.

Second you attempt to use the address of b which is a local variable and its scope ends with the function func, you can not use it outside.

This code will work:

#include <stdio.h>
#define SIZE 10

void func (char *array[], char *b)
{
    array[0]=b;
}

int main () 
{
    char *array_in_main[SIZE];
    char b_in_main='b';
    func (array_in_main, &b_in_main);

    return 0;
}
Osiris
  • 2,783
  • 9
  • 17
  • ok but in this way is array_in_main still modified when func() ends in the main? I don't understand if pass array in function is like pass a simple variable or not. – ilacollie Aug 30 '18 at 14:26
  • @ilacollie Yes it is still modified. – Osiris Sep 02 '18 at 17:28
1

Arrays in C are broken down into a pointer to the first element when used as a parameter.

Just pass the array itself. There's no need for a pointer to it.

Also, like Osiris said, you can't use the address of a local variable outside its scope.

void func (char *array[], char b){  /* Changed &b to b */
    array[0][0]=b; /* Changed array[0] to array[0][0], assuming that array is a 2D array */
}
int main () {
    char *array_in_main[SIZE];
    char b_in_main='b';
    func (array_in_main, b_in_main); /* Changed *array_in_main to array_in_main */
    return 0;
}
absoluteAquarian
  • 510
  • 3
  • 12
0

This is probably closer to what you're trying to do, no?

In addition to the compilation issues (already explained in Tau's answer and in Osiris' answer), it's worth noting that arrays are already pointers themselves.

Just declare in main char array_in_main[SIZE]. Afterwards you can just specify char array[] as the argument and pass array_in_main directly. What is passed to the function is the address of the first position in the array &(array_in_main[0]), so the array can be effectively updated from the function.

The code:

#define SIZE 10

void func(char array[], char b) {
  array[0] = b;
}

int main () {
  char array_in_main[SIZE];
  char b_in_main='x';
  func(array_in_main, b_in_main);
  return 0;
}
João Neto
  • 1,732
  • 17
  • 28
0

You need to understand the mechanism of how an array is passed to a function in C programming language. When you pass an array to a function in C, pointer (address) to first element of the array is passed to the function and since you have a reference to it, you can modify the content of the array in the function (but you can't change the memory location to which array is pointing). I recommend to have a look at this question.

The program below has two functions. rightFunc function is a simple and correct approach to achieve what you have been trying to do. However, I have provided func which shows the way of passing pointer (or reference) to an array of pointers.

#include <stdio.h>

#define SIZE 100

void rightFunc(char *array[], char *b)
{
    array[0] = b;
}

void func(char *(*array)[], char *b)
{
    (*array)[0] = b;
}

int main(int argc, char *argv[]) 
{
    char *array_in_main[SIZE];
    char b_in_main = 'b';

    func(&array_in_main, &b_in_main);
    printf("Character is %c\r\n", *array_in_main[0]);

    b_in_main = 'c';
    rightFunc(array_in_main, &b_in_main);
    printf("Character is %c\r\n", *array_in_main[0]);
    return 0;
}

I would like to point out one error in your program. You can get address of local variable (automatic storage class) but that address will not be valid outside the function (when function has exited). To rectify the issue in your program, I have modified the functions to take pointer to char in your program.

abhiarora
  • 9,743
  • 5
  • 32
  • 57