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!

2
  • 2
    Welcome to Stack Overflow. What part do you need help with? What are the errors and warnings you get? Commented Aug 28, 2018 at 16:10
  • 1
    You can't use the address of a local variable outside of its scope. Commented Aug 28, 2018 at 16:18

4 Answers 4

6

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 Yes it is still modified.
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;
}

2 Comments

array[0][0]=b; The pointer was never initialized, so i think this leads to undefined behavior.
true, though i did use *char[] for 2d char arrays often, hence my reasoning
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;
}

Comments

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.

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.