0

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;
}
asked Feb 10, 2013 at 19:28
12
  • 3
    The types of void (*fptr)(int array, int SIZE) don't seem the same of void sort(int a[], int size). Commented Feb 10, 2013 at 19:30
  • @Jack They may not seem to be the same, but they are. Commented Feb 10, 2013 at 19:31
  • @H2CO3 and int and a pointer to int? Actually I pointed out your same issue so I don't see your point. Commented Feb 10, 2013 at 19:32
  • @H2CO3 help the slow guy (me). When is an int synonymous with an int* (intptr_t not withstanding =P) Commented Feb 10, 2013 at 19:32
  • @H2CO3 had to qualify that, or I just knew you'd bring it to the table =P Commented Feb 10, 2013 at 19:33

1 Answer 1

7

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);
answered Feb 10, 2013 at 19:30
Sign up to request clarification or add additional context in comments.

9 Comments

H2CO3, thanks but its still not working.. Here is what I get: sortpointeragain.c:9: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant sortpointeragain.c:10: error: ‘fptr’ undeclared (first use in this function) sortpointeragain.c:10: error: (Each undeclared identifier is reported only once sortpointeragain.c:10: error: for each function it appears in.)
@user2059456: did you notice that your second parameter here: void (*fptr)(int array, int SIZE) has the same name of a #define? What happens is that preprocessor replaces it to void (*fptr)(int array, int 10) which is obviously not correct. Use a different name.
Yeah I tried to define SIZE to be 10 and use it as size of the array.. I'll try using another name.. get back to you in a second
Jack man you rock. You were right!!! But why can't I do such a thing.. I'm defining SIZE to be 10 which is an int and the pointer to function is expecting an int so it should be fine right???
OOOOHHH I see, the pointer is expecting a VARIABLE of type integer not just a number thrown in..... Am I right?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.