Hi
I'm new to c language i hava a problem :
i want to send a 2-d array to a function via pointer.
The function should return pointer to 2-d array.
I wrote the following code for this :
#include<stdio.h>
int* put(int *b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int *b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i compile it with gcc2de.c it shows following errors :
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:28: error: subscripted value is neither array nor pointer
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
Than i just change the code of function which is following :
#include<stdio.h>
int* put(int **b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int **b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i complie it i got following errors:
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
what I'm doing wrong ?
can anybody tell me what is the way to pass 2d-array via pointers to a function ?
can anybody tell me how to return two d array via pointer in a function
The first error that you have is that you are not passing a correct type as declared by your function. So to clean up your code with the least amount of corrections, it would probably look something like this:
#include<stdio.h>
void put(int *b);
int main()
{
int a[2][3],i,j;
put(&a[0][0]);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d", i, j, a[i][j]);
}
}
printf("\n\n");
system("PAUSE"); // Not recommended, but works for now
return 0;
}
void put(int *b)
{
int count = 1;
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
//b[i][j]=i;
*(b + ((i*3) + j)) = count++;
}
}
}
The two major corrections are:
You pass in the start address of your 2-D array explicitly by addressing it as &a[0][0].
Also, note the pointer arithmetic that you'll have to use when you use an int *b as well.
Note also that since you're passing in a pointer, you're modifying the value at that address location. Thus there is no need to return a pointer back at all.
Hope it helps. Cheers!
where are you storing the return value from put ?
the declaration should be int** put( int **) according yo your code.
The first error you have is that you are trying to define a function inside another function. The simplest thing to do is to just define put where you declare it:
int put()
{
/* definition of put */
}
int main()
{
/* body calls put */
}
The second problem is that in neither code snippet are you passing a compatible parameter to put.
If you want to pass a to a function then you should note that arrays as arguments always decay to a pointer to their first element.
a has type int [2][3], i.e. an array of 2 arrays of 3 ints. This will decay to a pointer to an array of 3 ints or int (*)[3]. This should explain the compile error that you are getting. You should declare put either as:
void put(int (*b)[3]);
or as the completely equivalent:
void put(int b[][3]);
Because you cannot pass arrays by value the compiler will automatically convert a function declaration which takes an array parameter to one which takes the equivalent pointer parameter.
I've changed the return type to void as you don't use or need the return value as you are passing the parameter by pointer. You should remove return b; from your definition of put.
Hint: Don't think of int[2][3] as a 2-d array but as an array of arrays.
You can not return a 2d-array from a function in C, you can only return a 1d-array of pointers to the first elements of a 2d-array.
Maybe you could find useful this:
Pass 2d array to function in C?
or this:
C++ Returning multidimension array from function
1.you should declare or define the function before use it,it's different with other popular luanguage.
2.you do not need to return the pointer in the function put ,the data in array has be changed
3.you needed to notice the type ,the type of int array[][] is int **
Related
a quick question. I'm trying to write a function which recieves a name of a string and prints its second character, But it won't compile (on http://ideone.com), can you address the issue? I don't see the problem as I am sending an address into the function, and asking to access the character in that address.
#include <stdio.h>
int main(void) {
char line[4] = "abc";
test(line);
return 0;
}
void test(int point) {
point++;
printf("%c",*point);
return;
}
The compile error that I am getting is -
Compilation error time: 0 memory: 10320 signal:0
prog.c: In function ‘main’:
prog.c:5:2: warning: implicit declaration of function ‘test’ [-Wimplicit-function-declaration]
test(line);
^~~~
prog.c: At top level:
prog.c:9:6: warning: conflicting types for ‘test’
void test(int point) {
^~~~
prog.c:5:2: note: previous implicit declaration of ‘test’ was here
test(line);
^~~~
prog.c: In function ‘test’:
prog.c:11:14: error: invalid type argument of unary ‘*’ (have ‘int’)
printf("%c",*point);
^~~~~~
Two problems here.
First, you use the function test before it's declared. So the function is implicitly declared as taking an unspecified number of arguments and returning int, i.e. int test(). This conflict with the definition void test(int point) that appears later.
The second issue is that you pass a char array (which decays to a char *) to test, but the function is expecting an int.
Move the definition of test to before main so that is defined before it's used, and change the point parameter from int to char *.
#include <stdio.h>
void test(char *point) {
point++;
printf("%c",*point);
return;
}
int main(void) {
char line[4] = "abc";
test(line);
return 0;
}
1) It wants a function prototype or for the function to be declared above your main.
2) The parameter point in
void test (int point)
is a pointer, not an int.
I have the following versions of passing 2D array as pointer.
Version 1
#include <stdio.h>
void disp(int a[][5])
{
printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}
int main ()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
disp(a);
return 0;
}
Version 2
#include <stdio.h>
typedef void(*callDisplay)(int*);
void disp(int a[][5])
{
printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}
int main ()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
callDisplay fn = (callDisplay) &disp;
fn(a);
return 0;
}
Version 1 rises warning incompatible pointer type. expected int (*)[5] but argument is of type int * as expected. However, (Version 2) calling the same function with pointer is compiling without any such warnings.
gcc options: gcc -O0 -g3 -Wall -c -fmessage-length=0
Could somebody pass light on this?
If you remove the cast when assigning the function pointer you get:
tmp.c: In function ‘main’:
tmp.c:13:22: warning: initialization from incompatible pointer type [enabled by default]
callDisplay fn = &disp;
The cast is suppressing this warning even though by casting to a function pointer of a different type you have invoked undefined behavior when you call the function pointer. Basically, you should never need to cast a function pointer as it will hide any warnings like this.
If you fix the function pointer you get the following code:
typedef void(*callDisplay)(int[][5]);
void disp(int a[][5])
{
printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}
int main ()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
callDisplay fn = &disp;
fn(a);
return 0;
}
Which when you compile you get the same warning as your first example:
tmp.c: In function ‘main’:
tmp.c:14:5: warning: passing argument 1 of ‘fn’ from incompatible pointer type [enabled by default]
fn(a);
^
tmp.c:14:5: note: expected ‘int (*)[5]’ but argument is of type ‘int *’
This function declaration
typedef void(*callDisplay)(int*);
has compatible argument when is called like
fn(a);
The problem is related to this casting
callDisplay fn = (callDisplay) &disp;
it is wrong.
That is the program has undefined behaviour.
According to the C Standard (6.3.2.3 Pointers)
8 A pointer to a function of one type may be converted to a pointer to
a function of another type and back again; the result shall compare
equal to the original pointer. If a converted pointer is used to
call a function whose type is not compatible with the referenced type,
the behavior is undefined.
Hi im trying to use a function to initialize an array with random number. somehow i get this error that i couldnt be able to solve
void arrayInit(int *A, int n){
int i;
for ( i = 0; i < n; ++i)
{
A[i] = rand();
}
}
call arrayInit() in main
int main(void){
int array1[1000];
arrayInit(&array1, 1000);
return 0;
}
I get error saying:
csort.c:62:13: warning: passing argument 1 of ‘arrayInit’ from incompatible pointer type
arrayInit(&array1, 1000);
^
csort.c:8:6: note: expected ‘int *’ but argument is of type ‘int (*)[1000]’
void arrayInit(int *A, int n){
^
You need the call for arrayInit to be like this:
arrayInit(array1, 1000);
Instead of this:
arrayInit(&array1, 1000);
The name of an array decays to be a pointer to the first element, which is what your function needs.
When you have
int array1[1000];
The type of &array1 is int (*)[1000], which is not what arrayInit expects. By using
arrayInit(arra1, 1000);
you are letting compiler decay the array to a pointer, which of of type int* in your case.
An array does not decay to a pointer in couple of cases:
When you use the & operator.
When you use the sizeof operator.
The identifier of any array is a type * itself.
So you need to call your function like this:
arrayInit(array1, 1000);.
Calling your function with an address of a pointer(&array1) would require a int** A(a pointer to a pointer type) in your function definition, which is certainly not the thing to do in this case, since the indexing in this case would also give an error.
you can call arrayInit() using the '&' as:
arrayInit(&array1[0], 1000);
I have an array:
char gameBoard[6][6];
// Initilize the array
for(int i = 0; i < 6; i++) {
for(int o = 0;o < 6;o++) {
gameBoard[i][o] = ' ';
}
}
Later in my code I have a function I am trying to use that will modify this array:
void placePiece(char piece, char *gameBoard) {
int x = 0;
int y = 0;
gameBoard[posXInArray][posYInArray] = piece;
}
I am calling the function from the same scope as where the gameBoard array is created and initialized.
placePiece('X', gameBoard);
The warning I get doing this:
warning: passing argument 2 of ‘placePiece’ from incompatible pointer type [enabled by default]
placePiece('X', gameBoard);
^
note: expected ‘char *’ but argument is of type ‘char (*)[6]’
void placePiece(char piece, char *gameBoard);
I am confused as to what I am doing wrong? I assumed to modify the array from another function I would pass the pointer so that I could directly modify the array. Is this incorrect? I have also tried &gameBoard in the call with the same warning message. I'm guessing I am doing something silly that is incorrect?
You can't pass the 2-dimensional array to this function
void placePiece(char piece, char *gameBoard)
I think this would work fine for you
void placePiece(char piece, unsigned int size, char gameBoard[size][size])
you can use it this way
placePiesce('X', 6, gameBoard);
or better
placePiesce('X', sizeof(gameBoard) / sizeof(gameBoard[0]), gameBoard);
and since the type of gameBoard[0] is char this will also work
placePiesce('X', sizeof(gameBoard), gameBoard);
because sizeof(gameBoard[0]) == 1 in that case.
hi I'm attempting to create a program that accepts a 7 element array as an argument and returns the third through fifth element of that array to a smaller array however i'm currently getting this error
assign8p7.c: In function 'main':
assign8p7.c:18:2: warning: passing argument 1 of 'copysect' makes pointer from
integer without a cast [enabled by default]
assign8p7.c:3:6: note: expected 'int *' but argument is of type 'int'
from what i can tell the warning has a problem with me passing it an array in the arguments does anyone know how i might fix this? also any other advice for my code is welcome.
#include <stdio.h>
int *copysect(int ar[],int start,int end)
{
int i;
static int retar[3];
for(i = 0; i<3;i++)
{
retar[i+start]=ar[i+start];
}
return retar;
}
int main(int argc, char const *argv[])
{
int arry[7] = {1,2,3,4,5,6,7};
int miniarry[3];
miniarry[0] = *copysect(arry[0],3,5);
return 0;
}
int *copysect(int ar[],int start,int end)
Okay, copysect takes as its first parameter an array of integers.
miniarry[0] = *copysect(arry[0],3,5);
Oops, you passed it a single integer instead of an array.
You are calling the function copysect with the first element in the array, not the pointer to the array. The correct call is:
copysect(arry,3,5);
You could calculate the difference of the array dynamically. Now the caller of copysect function has to know that the difference between start and end is 2.
int retar[end - start + 1]
The assignment in the for loop is wrong. You are dereferencing a value that is out of scope of retar array
retar[i]=ar[i+start];
When calling the copysect function, you are assigning only the first element in the miniarry by dereferencing the array that the function returns, instead of the whole array.
It's not the best idea to have a static array in a function (that would be problematic if you called the function more than once, etc). Instead, you could declare the smaller array elswhere and pass it as a parameter to the function.
void copysect(int ar[], int retar[], int start,int end, )