I'm currently studying pointers to functions and have been practicing on sort array functions.
The point is I input a sequence of numbers into the function and the program will re arrange it in ascending order. It worked just fine when I do a call by value function (I think that's how you call it). However when I try to assign a pointer to function and try to use that pointer instead of the function itself, it returns a bunch of errors. I'm sure the problem is due to the fact that I'm passing an array as an argument to the function POINTER. Here is my code:
#include<stdio.h>
#define SIZE 10
void sort(int a[], int size);
void swap(int *elt1, int *elt2);
main()
{
int i; int array[SIZE]= {1,9,3,2,4,100,43,23,32,12};
void (*fptr)(int array, int SIZE);
fptr = &sort;
(*fptr)(array,SIZE);
/*sort(array, SIZE);*/
for(i=0;i<SIZE;i++)
{
printf("%d\n", array[i]);
}
return 0;
}
void sort(int a[], int size)
{
int pass, j;
for(pass = 0; pass<size;pass++)
{
for(j=0;j<size;j++)
{
if(a[j]>a[j+1])
{
swap(&a[j], &a[j+1]);
}
}
}
}
void swap(int *elt1, int *elt2)
{
int hold;
hold = *elt1;
*elt1 = *elt2;
*elt2 = hold;
}
The first argument of the function is a pointer to int (that is, int *), and not int.
void (*fptr)(int array, int SIZE);
should be
void (*fptr)(int *array, int SIZE);
Related
#include<stdio.h>
struct Ques
{
int a;
}Q[5];
void sort(int a[])
{
printf("any sort technique...");
}
void main()
{
sort(Q.a);
}
So this is the sample code.
I Want to access the whole struct element as array.
You want this:
#include<stdio.h>
struct Ques
{
int a;
} Q[5];
void sort(struct Ques array[], int size)
{
printf("any sort technique...");
// just some demo
for (int i = 0; i < size; i++)
printf("array[%d].a = %d\n", i, array[i].a);
}
int main()
{
// put some data into Q
sort(Q, 5);
}
sort needs two parameters:
the pointer to the array to sort
the size of the array (unless you only ever want to sort array of some fixed size
This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 6 years ago.
So int size does not actually give me the size of what I pass in, in the main function. How do I get the exact number of elements in the array that I pass in.
void myFunc(int *array)
{ int size = (sizeof(&array)/sizeof(int));}
int main()
{
int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
myfunc(&array);
return 0;
}
Pass it as parameter to myFunc.
#include <stdio.h>
void myFunc(int *array, size_t size)
{
printf("Size = %zu\n", size);
for (size_t i=0; i<size; i++)
{
printf("array[%zu] = %d\n", i, array[i]);
}
}
int main(void)
{
int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
myFunc(array, sizeof(array)/sizeof(array[0]));
return 0;
}
main signature must be int main(void)
sizeof return type size_t so use that type for variables.
You have a typo in your code into main: myfunc should be myFunc
The function can't magically know, so it's usually solved something along this:
void myFunc(int *array, int size)
{
int i;
for(i=0;i<size;++i)
array[i] = i;
}
#define ARRSZ(arr) arr, sizeof(arr)/sizeof(*arr)
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
myFunc( ARRSZ(array) );
return 0;
}
I am confused in C as i am newbie. i know 1.1 is giving me maximum value and 1.2 is giving me maximum value variable address [Picture].
My question is how do i call *findmax function in main?
int * findMax(int *a,int SIZE){
int i,max=*a,address,add;
for(i=0;i<SIZE;i++){
if(max<*(a+i)){
max=*(a+i);
}
}
//printf("maxium value is %d at index %x",max,&max);
return &max;
}
The * in the function definition is not a function pointer, it's the function's return type. The findMax function returns a pointer to integer. So you would call it just like any other functions in main:
int a[] = {1,2,3,4};
int *p = findMax(a, 4);
There is another problem, in your findMax function, you returned a pointer to a local variable, the storage of the variable will be no longer available when the function returns. Use it causes undefined behavior. So you can just return the max as an integer instead, if you really need to return a pointer, you should allocate it, or return a pointer that remains valid.
For example:
int* findMax(int *a,int SIZE){
int i;
int *max = a;
for(i=0;i<SIZE;i++){
if(*max<*(a+i)){
max=a+i;
}
}
return max;
}
#include<stdio.h>
int Max;
int* FindMax(int *a,int size)
{
int i;
Max=a[0];
for(i=0;i<size;i++)
{
if(Max<=a[i])
Max=a[i];
}
return &Max;
}
int main()
{
int a[10]={10,19,9,127,45,189,47,222,90,158};
printf("Address of Max Element:%p \n",FindMax(a,10));
printf("Max of Elements:%d \n",Max);
getchar();
return 0;
}
I'm trying to print array of pointer using pointer instead of array but I got this error Segmentation fault at runtime:
enter number of element:5
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
Segmentation fault
This is the code:
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int *array,int n);
void display(int *array,int n);
int sum(int *array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(array,n);
display(array,n);
result=sum(array,n);
printf("sum of array=%d",result);
return 0;
}
void input(int *array,int n){
int j;
array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",array+j);
}
}
void display(int *array,int n){
int j;
for(j=0;j<n;j++)
printf("%d\t",*(array+j));
printf("\n");
}
int sum(int *array,int n){
int sum=0,j;
for(j=0;j<n;j++)
sum+=*array+j;
return sum;
}
How can I fixed this code? please somebody explain me what's wrong with that code.
Variable array is a local variable in function input.
As such, it is pointless to set it with array = ..., because this assignment takes effect only inside the function. You should typically pass its address (&array) to any function that needs to change it.
In your specific example, you also have a global variable array, so a quick solution to your problem would be to simply call function input without passing variable array as an argument:
void input(int n)
{
...
array = (int*)malloc(n*sizeof(int));
...
}
int main()
{
...
input(n);
...
}
Note that this is a "dirty" workaround, and you should typically strive to avoid the use of global variables.
To add the clean version to barak's answer:
int input(int ** array, const size_t n)
{
int result = 0;
assert(NULL != array);
(*array) = malloc(n * sizeof(**array));
if (NULL == (*array))
{
result = -1;
}
else
{
size_t j;
for(j = 0; j < n; ++j)
{
printf("array[%zu]=", j);
scanf("%d", (*array) + j); /* still missing error checking here . */
}
}
return result;
}
And call it like this:
if (-1 == input(&array, n))
{
perror("input() failed");
exit(EXIT_FAILURE);
}
Try this input():
void input(int **array,int n){
int j;
*array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",*array+j);
}
}
Because C use pass-by-value, if you want to change the value of a variable in a function, you need to pass the address of that variable as the argument to that function.
In this case, you want to change the value of array in input() and the type of array is int *, therefore the prototype of input() should be something like void input (int **array, ...).
this should do..make sure you understand what the others have said..
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int **array,int n);
void display(int **array,int n);
int sum(int **array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(&array,n);
display(&array,n);
result = sum(&array,n);
printf("sum of array= %d",result);
return 0;
}
void input(int **array,int n){
int j;
*array= malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);
scanf("%d",(*array)+j);
}
}
void display(int **array,int n){
int j;
for(j=0;j<n;j++){
printf("%d\t",*((*array)+j)); // you can use array notation aswell
//array[0][j] will work
}
printf("\n");
}
int sum(int **array,int n){
int sum=0,j;
for(j=0;j<n;j++){
sum += *((*array)+j);
}
return sum;
}
What does *array + j do? Does it evaluate *array and add j to it? Or does it add j to array and then dereference it? Would you be willing to bet $100 on it if I told you you are wrong?
Make your life and the life of anybody reading your code easier by using parentheses, or even better, write array [j].
I've been reading this question but I'm not able to get the resulting code to solve the problem.
How should I change this in order to make it work?
void print2(int ** array,int n, int m);
main()
{
int array[][4]={{1,2,3,4},{5,6,7,8}};
int array2[][2]={{1,2},{3,4},{5,6},{7,8}};
print2(array,2,4);
}
void print2(int ** array,int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
You are passing in a pointer to an array, but your function is expecting a pointer to a pointer. In C, the array name decays to a value that is the pointer to the first array element. In this case, the first array element is an array, so the function argument decays to a pointer to an array.
Here is one way you can fix this. Change the function to take a void * so that the dimension does not interfere with the argument. Then the dimension argument is used in the function body to create the proper pointer type for the 2D array.
void print2(void *p,int n,int m)
{
int i,j;
int (*array)[m] = p;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
If you are willing to change the order of the arguments, then you can use the proper type for the array argument:
void print2(int n, int m, int array[n][m])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
Since Jack asked about C89, here's a way to handle it. Since a 2D array is organized the same as a long 1D array in memory, you can just walk the passed in pointer as such. Again, we accept the input parameter as a void * to avoid dealing with the decayed type. Then, we treat the pointer as a long 1D array, but we walk it according to the proper dimensions:
void print2(void *p, int n, int m)
{
int i,j;
int *array = p;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i*m+j]);
printf("\n");
}
}
In your question you are passing arguments as pointer to an array.
Do as given below :
void print2(int (*array)[4],int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}