Pointer to a function?

looking for clarity with printing out a function using a pointer

Page 1 of 1

13 Replies - 1043 Views - Last Post: 06 March 2010 - 10:09 PM Rate Topic: -----

#1 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Pointer to a function?

Posted 05 March 2010 - 03:42 PM

I'm working on a programming exercise to write functions in C.

The main program takes an array, and prints out the values in reverse order. So if I have an array of ten elements it prints out the last one first and so on. The main program calls the function in a weird way and I'm not sure how to make it work right. Here's the function call:

printf("%2d ",*revElement(a));



my code for the function is:
int revElement(int theArray[]){
     
     static int place = N;
     --place;
     
     return theArray[place];
}



my code works with the print statment only if I remove the pointer.

Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to a function?

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Pointer to a function?

Posted 05 March 2010 - 04:05 PM

Now, I'm no C expert (so I could be wrong in some way here)...but I'm fairly certain that the pointer doesn't belong. A function call is a function call. I know there are some weird ways to do function pointers (and I don't know them, just remember reading them a while back), but this isn't one of them. This is a standard function.

printf("%2d", revElement(a));
Was This Post Helpful? 0
  • +
  • -

#3 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Pointer to a function?

Posted 05 March 2010 - 04:15 PM

That's what I thought! In fact when I run it the way my prof has it written I get a compiler error.

View PostLocke, on 05 March 2010 - 03:05 PM, said:

Now, I'm no C expert (so I could be wrong in some way here)...but I'm fairly certain that the pointer doesn't belong. A function call is a function call. I know there are some weird ways to do function pointers (and I don't know them, just remember reading them a while back), but this isn't one of them. This is a standard function.

printf("%2d", revElement(a));

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Pointer to a function?

Posted 05 March 2010 - 04:49 PM

The way the * operator is used here is usually called dereferencing.
printf("%2d ",*revElement(a));



int first = 6;
int *ptr;   

ptr=&first;  // ptr = address of first

printf("%d", *ptr); // print the value pointed to by ptr.


Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pointer to a function?

Posted 05 March 2010 - 05:03 PM

The function doesn't return a int* so the dereference isn't necessary.

Since you're having compiling issues, post the snippet you're trying to compile along with specific error messages please.
Was This Post Helpful? 0
  • +
  • -

#6 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Pointer to a function?

Posted 05 March 2010 - 06:00 PM

Here are the code snippets I'm trying to compile, let me know if I should just post the whole program:

   printf("Original: ");
   for (idx = 0; idx < N; idx++) {
          printf("%2d ", *revElement(a));
   }

int revElement(int theArray[]){
     
     static int place = N;
     --place;
     
     return theArray[place];
}




And this is the error I'm getting - invalid type argument of 'unary *'
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pointer to a function?

Posted 05 March 2010 - 06:16 PM

Like my post before, your function returns an integer. The derefernece is not valid.
Was This Post Helpful? 0
  • +
  • -

#8 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Pointer to a function?

Posted 05 March 2010 - 06:21 PM

revElement returns an int, and in the way the * is used here would suggests it returns a int pointer. Combining a pointer with the * operator is called dereferencing and will give you the int value of the adress the pointer is pointer to. But since this isn't a pointer to an adress but a plain int, the * makes no sense. Hope I can make myself clear :)

Edit: maybe your prof wanted you to make the revElement function return a pointer to a specific location in the array, that would explain the dereference

This post has been edited by Renagado: 05 March 2010 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Pointer to a function?

Posted 05 March 2010 - 06:58 PM

Thanks Renagado, I think I see what your saying.

So if I wanted to return the pointer I could rewrite revElement to first point to the last address of the array and declare a static variable that would subtract 1 address each time it's called?

This post has been edited by thefitgeek: 05 March 2010 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#10 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Pointer to a function?

Posted 05 March 2010 - 07:13 PM

Yes you could declare a static int pointer, make it point to an address(thats the & operator) of an element in your array and then return that pointer. That way when you use the * operator in your function call you get the value of what the pointer is pointing to.

You could also write the pointer to console without the *, but then you get the memory address to where it points. Try that out, it's interesting.
Was This Post Helpful? 0
  • +
  • -

#11 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Pointer to a function?

Posted 05 March 2010 - 07:38 PM

I made changes to my code to declare a static pointer and point it to the last address in the array. It compiled fine except I'm still getting the error invalid type argument of 'unary *'

Actually went back and made more changes to my function declaration and header. I also changed the return value. Now my code appears to be accessing values outside the array


int *revElement(int theArray[]){
     
     static int *n;
	n = &theArray[(N-1)];
     *n--;
     
     return n;
}



This post has been edited by thefitgeek: 05 March 2010 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Pointer to a function?

Posted 05 March 2010 - 09:27 PM

You need to remember where you are so I think the place variable was a good idea.

Have a look at Pointers in particular at Pointers and arrays, and possibly Pointer arithmetics.
Was This Post Helpful? 0
  • +
  • -

#13 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Pointer to a function?

Posted 06 March 2010 - 06:05 AM

You removed the initialisation and declaration of big N this time. Or maybe you moved it? Anyway if you set it to the size of the array you should be fine. Also as a little suggestion, rename N to size or something like that, clear variable names will make things easier.
Was This Post Helpful? 0
  • +
  • -

#14 thefitgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-February 10

Re: Pointer to a function?

Posted 06 March 2010 - 10:09 PM

Thanks guys I got it to work this morning after reading #define's comment to remind me about the place variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1