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++;
}
Related
I'm having trouble passing an integer array as a reference, and then modifying the original array.
#include <stdio.h>
// sets the 2 element of i to 5
void setToFive(int *i[10]){
*i[2] = 5;
printf("hello\n");
}
int main(){
int i[10];
setToFive(&i);
// confirm i[2] == 5
printf("%d\n", i[2]);
}
Compiler complains about invalid type
[vinessa#komputilo ch1]$ gcc test.c
test.c: In function ‘setToFive’:
test.c:5:5: error: invalid type argument of unary ‘*’ (have ‘int’)
5 | *i[2] = 5;
| ^~~~~
test.c: In function ‘main’:
test.c:11:15: warning: passing argument 1 of ‘setToFive’ from incompatible pointer type [-Wincompatible-pointer-types]
11 | setToFive(&i);
| ^~
| |
| int (*)[10]
test.c:4:21: note: expected ‘int *’ but argument is of type ‘int (*)[10]’
4 | void setToFive(int *i){
| ~~~~~^
segmentation fault
[vinessa#komputilo ch1]$ ./a.out
Segmentation fault (core dumped)
Have been banging head at problem for hours, please help.
Here you are
#include <stdio.h>
// sets the 2 element of i to 5
void setToFive(int *i){
i[2] = 5;
printf("hello\n");
}
int main(){
int i[10];
setToFive(i);
printf("%d\n", i[2]);
}
If you want to change elements of an array then just pass it by value. In this case the array designator is implicitly converted to pointer to its first element. Using the pointer and the pointer arithmetic you can change any element of the array.
In fact in this case you are passing elements of the array by reference indirectly through a pointer to them.
Pay attention to that these function declarations
void setToFive(int i[100]);
void setToFive(int i[10]);
void setToFive(int i[1]);
void setToFive(int i[]);
are equivalent and declare the same one function declaration of which the compiler adjusts to the following declaration
void setToFive(int *i);
That is as a result the function deals with a pointer.
As for the expression used as an argument in this function call
setToFive(&i);
then it has the type int ( * )[10] because the pointed array is declared like
int i[10];
It is not the same as the type of the parameter int *i[10] that as I already wrote is adjusted by the compiler to the type int **.
You are passing an array of pointers to integers, not an array of integers. You can just remove the various * declarations and operators, and just pass a simple array as the argument (the array argument will decay to a pointer):
#include <stdio.h>
// sets the 2 element of i to 5
void setToFive(int i[10])
{
i[2] = 5;
printf("hello\n");
}
int main()
{
int i[10];
setToFive(i);
// confirm i[2] == 5
printf("%d\n", i[2]);
return 0;
}
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);
I'm new in programming and learning pointers in array in C. Have a look at the below programmes.
1st program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int **p)
{
*(*(p+1)+1)=2135;
return 0;
}
2nd program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int *p)
{
*((p+1)+1)=2135;
return 0;
}
3rd program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int (*p)[3])
{
*(*(p+1)+1)=2135;
return 0;
}
In the first program **p is used in the fun() function which I think it should be correct and in that function I've written *(*(p+1)+1) to change the first element of first array. But on compiling this program it's showing error: invalid type argument of unary '*' (have 'int'). As far as I know num is a pointer to array and it is holding the address of num[1] which is again holding the address of num[1][0].
On compiling the second program compiler is showing no error. And *((p+1)+1)=0 is changing the value of 2nd element of first array. Why it is changing the value of 2nd element of zeroth array not the value of first element of first array? and How? It should be *(*(p+1)+1)=0.
In the third program the compler is showing no error and it is showing the correct result. How?. What does *(p)[3] mean?
I had searched about this but couldn't found the satisfactory result.
All of your programs are ill-formed. Your compiler must produce warning or error messages, and the output of any executable produced is meaningless.
They are ill-formed because int[3][3] is not compatible with int **, nor with int *, nor with int *[3].
To pass int[3][3] to a function, the function must accept int (*)[3] and nothing else (well, except for void *).
This is because arrays can be converted to a pointer to the first element of the array. (In C syntax, num can be used to mean &num[0]).
In C, there are only truly one-dimensional arrays; an array of type int[3][3] is considered to be an array of 3 elements, each of which is an array of 3 ints.
So a pointer to the first element of num is a pointer to an array of 3 ints, which is written as int (*p)[3]. You could write:
int (*p)[3] = &num[0];
or the shorthand for the same thing:
int (*p)[3] = num;
NB. You continually write *(*(num+1)+1)) which is difficult to read. Instead of this, num[1][1] seems much clearer.
In C, x[y] is always exactly equivalent to *(x+y).
I think you are asking: What's the difference between
int fun(int *p)
and
int fun(int (*p)[3])
The first one expects a pointer to an int. The second one expects a pointer to an array of 3 ints.
You are able to call to both these functions using num since you declared the function as
int fun();
If you declare the functions like they are defined, you will get compiler error/warning for the first version.
Here's an updated version of your code and the resulting compiler warning, using gcc and compiler flag -Wall.
#include <stdio.h>
int fun(int *p);
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
return 0;
}
int fun(int *p)
{
*(p+0)=2135;
return 0;
}
test.c: In function ‘main’:
test.c:7:4: warning: missing braces around initializer [-Wmissing-braces]
test.c:7:4: warning: (near initialization for ‘num[0]’) [-Wmissing-braces]
test.c:8:4: warning: passing argument 1 of ‘fun’ from incompatible pointer type [enabled by default]
test.c:3:5: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
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.
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.