I'm trying to modify a multidimensional array. This is my function code -
void rot90(int n,char **a)
{
int i,j;
int b[n][n];
for(i=n-1;i>=0;i--)
{
for(j=0;j<n;j++)
{
a[n-1-i][j]=b[j][i];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=b[i][j];
}
}
}
And in my main function , I am calling it as -
A is a 2d Array nxn.
rot90(n,A);
which shows the following error on compilation -
warning: passing argument 2 of ‘rot90’ from incompatible pointer type [enabled by default]
note: expected ‘char **’ but argument is of type ‘char (*)[10]’
what is the right way to do it?
Thanks.
If A is an NxN array, and you're using a VLA (which it looks like from your snippet), then the prototype needs to be either
void rot90( int n, char (*a)[n] )
or
void rot90( int n, char a[][n] )
or
void rot90( int n, char a[n][n] )
The second two forms are interpreted the same as the first; a is a pointer to an n-element array of char.
Related
New learner here; I am performing a traverse on any given array but I find I get this error:
exe_3.c:18:27: warning: incompatible pointer types passing 'int *' to parameter of type 'int **' [-Wincompatible-pointer-types]
int result = traverse(&arr[6], &n);
^~~~~~~
exe_3.c:4:25: note: passing argument to parameter 'A' here
const int traverse(int *A[], int *N){
What I have tried:
#include <stdio.h>
#include <stdlib.h>
const int traverse(int *A[], int *N){
int i = 0;
int arr[*N];
while(i < *N){
arr[i] = *A[i];
i += 1;
}
return *arr;
}
int main(){
int arr[6] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
int result = traverse(&arr, &n);
printf("%i\n", result);
return EXIT_SUCCESS;
}
Your call traverse(&arr, &n); passes a pointer to the array arr to the function traverse.
You get the error message, because the correct type definition for a pointer to an array of integers is int(A*)[]. You have that type in the definition of the traverse function incorrect (your line 4).
You will see that this is not enough to compile your code. When accessing the elements of such an array via that pointer you need the expression (*A)[i]. You have that access in the implementation of the traverse function incorrect (your line 8).
See also here for more details: C pointer to array/array of pointers disambiguation
What I find also strange with your traverse function is that the array arr is not used completely. Only the first value is returned. I suppose your code is just not complete.
My compiler gives me this warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types] with this note: expected 'int (*)[10]' but argument is of type 'int **'
My code:
void transform_labels(int array[60000], int labels[60000][10], int NPAT){
for(int i = 0; i < NPAT; i++){
int aux = array[i];
labels[i][aux] = 1;
printf("%d\n ",*labels[i]);
if ((i+1) % 10 == 0) putchar('>');
}
}
int main() {
load_mnist();
int loop;
int** labels;
allocate_mem(&labels, 60000, 10);
printf("HERE");
transform_labels(train_label, labels, 60000);
return 0;
}
A pointer to a pointer cannot be converted to a pointer to an array. While an array can be converted to a pointer that only applies to the outermost dimension of a multidimensional array.
You need to change the declaration of your function:
void transform_labels(int *array, int **labels, int NPAT){
You are allowed to pass a pointer instead of the first dimension of a function argument, and vice-versa, but all other dimensions must match. You have a second dimension [10].
You can pass it a pointer to an array of size 10, but that might just push your problem up to another point in the code, such as your allocate function. The following should compile, but it is not clear that this is what you want:
typedef int LabelSub[10];
LabelSub* labels;
allocate_mem(&labels, 60000, 10);
In my main function i create a dynamic 3D array and succesfull alocate it.
int ***board = (int ***)malloc(counter * sizeof(int **));
for(int i = 0; i < counter; i++){
board[i] = (int **) malloc(dimension[i] * dimension[i] * sizeof(int));
}
Then i wan to pass it to a function defined as:
void readPuzzle(int board[][MAX][MAX], int *dimension, int counter,const char *);
I pass it like this:
readPuzzle(board, dimension, counter, argv[1]);
But i keep getting this error, i searched for it but never could fix it!
main.c: In function ‘main’:
main.c:22:13: warning: passing argument 1 of ‘readPuzzle’ from incompatible pointer type [-Wincompatible-pointer-types]
readPuzzle(board, dimension, counter, argv[1]);
^~~~~
In file included from main.c:4:0:
headers.h:7:6: note: expected ‘int (*)[100][100]’ but argument is of type ‘int ***’
void readPuzzle(int board[][MAX][MAX], int *dimension, int counter, const char *);
If your 2D arrays have variable dimensions, you need int ***, but this prototype
void readPuzzle(int board[][MAX][MAX], int *dimension, int counter, const char *);
isn't compatible with board passed as int *** because it required an array of 2D arrays, not a triple pointer.
So fix it like that:
void readPuzzle(int ***board, int *dimension, int counter, const char *);
but you still need to keep track of the dimensions for each "slice".
The alternative, if the 2D arrays have fixed dimensions MAX*MAX is to allocate an array of 2D arrays like this:
int (*board)[MAX][MAX] = malloc(counter*sizeof(*board));
now you can pass board as int board[][MAX][MAX]. Of course, if not all data is used, you still need to keep track of max dimensions somewhere.
I was doing this exercise and I had to write a program that takes in a list of numbers and swaps pairs of numbers so they're in order:
void swapPairs(int* a[], int length)
{
int i=0;
int temp;
while(i<(length-1))
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
i++;
}
}
int main()
{
int array[]={2,1,3,1};
swapPairs(array, 4);
return 0;
}
I keep getting these errors:
In function ‘swapPairs’:
warning: assignment makes integer from pointer without a cast
temp=a[i];
^
warning: assignment makes pointer from integer without a cast
a[i+1]=temp;
In function ‘main’: warning: passing argument 1 of ‘swapPairs’ from incompatible pointer type
swapPairs(array, 4);
^
note: expected ‘int **’ but argument is of type ‘int *’
void swapPairs(int* a[], int length)
^
When I tried it with just an array instead of a pointer, it worked perfectly fine. Can someone please explain what is wrong with this and how to fix it?
Thanks in advance.
Your declaration of swapPairs is wrong - it shouldn't accept an array of int * (int pointers) - it should accept and an array of ints:
void swapPairs(int a[], int length)
The type of 'temp' is int. The type of 'a[i]' is *int (pointer to an int).
You are assigning the value of a pointer rather than the value of an integer because you are failing to dereference the pointer.
The while loop should read:
while(i<(length-1))
{
if(*(a[i])>*(a[i+1]))
{
temp=*(a[i]);
*(a[i])=*(a[i+1]);
*(a[i+1])=temp;
}
i++;
}
Why the following code is giving segmentation fault error
#include<stdio.h>
int main()
{
int i;
int a[2][2]={1,2,3,4};
int **c;
c=a;
for(i=0;i<4;i++)
printf("%d",*(*(c)+i));
}
This assignment:
c=a;
Should give you a warning. a decays into a pointer to its first element, which has type int (*)[2]. Assigning that type to a variable of type int ** requires an explicit cast.
Redeclaring c should fix your problem:
int (*c)[2];
Example warning from clang:
example.c:8:6: warning: incompatible pointer types assigning to 'int **' from
'int [2][2]' [-Wincompatible-pointer-types]
c=a;
^~
1 warning generated.
Read the comments of the following code:
#include<stdio.h>
int main()
{
int i;
int a[2][2]={{1,2},{3,4}}; // Put each dimension in its braces
/*int a[2][2]={1,2,3,4};
This declaration of array make the following:
a1[ONE] a2[TWO] THREE FOUR
a3[Unknown value] a4[Unknown value]
i.e. the numbers 3 and 4 are being written beyond of the array...
*/
int *c1;
int **c2; // `int **` is a pointer to a pointer, so you have to
c1=&a[0][0]; // declare a pointer `c1` and then assign to it `c2`.
c2=&c1; // AND use `&` to assing to pointer the address of
// variable, not its value.
for(i=0;i<4;i++)
printf("%d",*(*(c2)+i)); // here is `double dereference` so here must be `c2`
// (ptr-to-ptr-to-int) but not c1 (ptr-to-int).
return 0; // AND make the `main()` to return an `int` or
// make the returning type `void`: `void main(){}`
// to make the `main()` function to return nothing.
}
This is a problem in the definition of c. int **c; Suggests that this is a pointer to a pointer, but the definition of a is of type int *[2]. Changing the definition of c to int (*c)[2] should do the trick.