Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to pass the B int array pointer into func function and be able to change it from there and then view the changes in main function

#include <stdio.h>

int func(int *B[10]){

}

int main(void){

    int *B[10];

    func(&B);

    return 0;
}

the above code gives me some errors:

In function 'main':|
warning: passing argument 1 of 'func' from incompatible pointer type [enabled by default]|
note: expected 'int **' but argument is of type 'int * (*)[10]'|

EDIT: new code:

#include <stdio.h>

int func(int *B){
    *B[0] = 5;
}

int main(void){

    int B[10] = {NULL};
    printf("b[0] = %d\n\n", B[0]);
    func(B);
    printf("b[0] = %d\n\n", B[0]);

    return 0;
}

now i get these errors:

||In function 'func':|
|4|error: invalid type argument of unary '*' (have 'int')|
||In function 'main':|
|9|warning: initialization makes integer from pointer without a cast [enabled by default]|
|9|warning: (near initialization for 'B[0]') [enabled by default]|
||=== Build finished: 1 errors, 2 warnings ===|
share|improve this question
 
Well, the error message explains it. You pass a pointer to an array of 10 int *, but func expects an int** (which is expected to be a pointer to the first element of an array of (10, presumably) int*s). How to fix it depends on what func does. –  Daniel Fischer Dec 5 '12 at 19:22
 
func will simply edit B values like B[0], B[1] etc.. –  fxuser Dec 5 '12 at 19:24
 
Then you probably want to pass just B. Since B is actually an array, passing &B is typically not useful, since B can't be changed (but its contents can be changed, and that's what you want to do). –  Daniel Fischer Dec 5 '12 at 19:26
 
updated question with new code with errors when i try to edit an value of B in func function –  fxuser Dec 5 '12 at 19:29
 
The answer to your original question is that func should be declared like this: int func(int (*B)[10]) –  newacct Dec 5 '12 at 21:04

3 Answers

up vote 4 down vote accepted

In your new code,

int func(int *B){
    *B[0] = 5;
}

B is a pointer to int, thus B[0] is an int, and you can't dereference an int. Just remove the *,

int func(int *B){
    B[0] = 5;
}

and it works.

In the initialisation

int B[10] = {NULL};

you are initialising anint with a void* (NULL). Since there is a valid conversion from void* to int, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.

int B[10] = {0};

is the proper way to 0-initialise an int[10].

share|improve this answer
 
Finally a correct answer... +1 –  Ed S. Dec 5 '12 at 19:41

Maybe you were trying to do this?

#include <stdio.h>

int func( int * B ){

    /* B + OFFSET = 5 () You are pointing to the same region as B[OFFSET] */
    *( B + 2 ) = 5;
}

int main( void )
{

    int B[10];

    func(B);

    /* Let's say you edited only 2 and you want to show it. */
    printf("b[0] = %d\n\n", B[2]);

    return 0;
}
share|improve this answer
 
if i try to edit B[2] in func i get some errors... code: *B[2] = 5; –  fxuser Dec 5 '12 at 19:27
 
@fxuser: "I get some errors" is not a useful problem description. Ask for help as if you were the person being asked. You are getting an error because B is a pointer to int and B[2] returns an int, not a pointer. So, you just want B[2] = 5; –  Ed S. Dec 5 '12 at 19:35
 
@fxuser I solved your problem take a look –  Alberto Bonsanto Dec 5 '12 at 19:38

In new code assignment should be,

B[0] = 5

In func(B), you are just passing address of the pointer which is pointing to array B. You can do change in func() as B[i] or *(B + i). Where i is the index of the array.

In the first code the declaration says,

int *B[10]

says that B is an array of 10 elements, each element of which is a pointer to a int. That is, B[i] is a int pointer and *B[i] is the integer it points to the first integer of the i-th saved text line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.