C++ :: Using Pointers With Function Both As Arguments And Return Type
Jan 29, 2015I always have confusions while using pointers with functions both as arguments and as return type.
View 1 RepliesI always have confusions while using pointers with functions both as arguments and as return type.
View 1 RepliesSay I have a function pointer with this definition:
void ( *pressFunc ) ( void*, void* );
And i did this function:
void functionWithOneArg ( void* testPtr );
And i did this
pressFunc = &functionWithOneArg;
One. Would C actually let me do this? ( Assigning a function with one argument to a function with two )
Two. If so, what would happen to the second argument that is passed the function when its called? Does it just get 'cut off' and only the first argument is passed?
Is there anyway we can return 2 values from a called function. Example
Code:
float xxxx(float a, float b, float c, float d)
{///
///
///
}
void xxx() {
int e,f,g,h;
////
////
xxx(e,f,g,h);
}
So if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming.
I need to create the following brain damaging abomination:
I need a function pointer type to a function that has an argument of the same function pointer type and returns the same function pointer type.
The purpose is to enable a type of subroutine threading scheme for a small application specific scripting language. The question could just as well have been posted to the C forum.
This syntax works, but Payload is a generic type which I can coerce into the right pointer type via a cast. This is ugly IMHO. I could also hide it as a pointer in the FlipState class since I've forward declared this.
But this is an extra indirection in a performance critical part of the code, and also ugly.
Code:
class FlipState ;
typedef PayLoad (*FuncPtr) (FlipState *fs, PayLoad P) ; This syntax blows chunks using gcc on the other hand. Code: class FlipState ;
typedef FuncPtr (*FuncPtr) (FlipState *fs, FuncPtr P) ;
[Code] .....
This is hardly surprising. The compiler could not possibly understand what I was defining in the typedef. I think what I need is some kind of way to forward declare a function pointer type and then redefine it properly.
Is such a think even possible or am I just SOL? This one is mind boggling. We know how to do this with classes or other complex data types, but the syntax eludes me for both C++ and C.
Say I have overloaded functions with different arguments AND return types. Each set of argument has only one corresponding return type.
Code:
Vector grad(Scalar)
Tensor grad(Vector)
Later I have:
Code:
template <class T>
void test(T x) {
... Y = grad(x)
}
Then how do I automatically declare the type of Y. Do I need to make the grad function a template and specialize each of them ?
i heard that it was int but later it is deprecated , so at present what is the default return type of a function ?
View 4 Replies View RelatedI want my function to return the type mpz_t, but I'm not sure how?
I've tried: mpz_t MyFunction(mpz_t A, mpz_t B){}
But it didn't work, here is my code so far, I have bolded the parts of the code which are causing errors and added the errors in the comments:
Code:
#include <iostream>
#include <mpir.h>
using namespace std;
???? A(mpz_t m, mpz_t n){
mpz_t mmo; //used to store the value of m, minus one
[Code] .....
Is there a way around this?
I am having problems with my function definition of a function that should return a structure value.
This is the error I get compute.cpp(9): error C2146: syntax error : missing ';' before identifier 's_advertisebus'
The error is on the line where I start my function definition typing my function type as a structure. A long time ago in c the keyword struct is used with the structure type like struct s_advertisebus s_readadbus(). I tried it both ways but I got errors.
// struct.h
#ifndef STRUCT_H
#define STRUCT_H
struct s_advertisebus {
int nnumberofads;
float fpercentused;
[Code] ....
If we are using strcpy() for copying the string. As we are passing pointers to it It will copy the string & no need to return the string .This function will finely work with return type as void then why Ritchie has used it as char* strcpy()?
View 4 Replies View RelatedIs this really the preferred way to get the return type, for use in a derived class, of a function defined in the template parameter?
template<class PARAMETER> class C {
protected:
typedef typeof (reinterpret_cast<PARAMETER*>(0))->function() returntype;
};
This works just fine for me, but seems inelegant.
#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;
int main(void) {
double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
double TC[4][4];
transpose(C, TC);
[Code] ......
I have this code:
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
[Code]...
and it does not compile.
The error is:
test.cpp: In function ‘int main()’:
test.cpp:20:30: error: no matching function for call to ‘func1(std::vector<int>&)’
test.cpp:20:30: note: candidate is:
test.cpp:8:45: note: template<class T, class U> std::map<T, T> func1(U)
test.cpp:8:45: note: template argument deduction/substitution failed:
test.cpp:20:30: note: couldn't deduce template parameter ‘T’
I'm having some problems with a function. The function is supposed to find the two largest values in an array.
Code:
void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;
[Code]....
I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.
Code:
int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;
[Code]...
Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?
Code:
void Class1::Func(shared_ptr<type1> parameter)
{
}
or
void Class1::Func(const shared_ptr<type1>& parameter)
{
}
or
Should I ever pass arguments/parameters to other objects using shared_ptr's or raw pointers?
Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?
I mean either using:
void ptr_Func(int *x)
{
*x = *x+1
}
or
int ptr_Func(int x)
{
return x + 1;
}
In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.
For example I want to get the type of std::vector<>::size_type without specifying a template argument.
Here's what I want to do:
std::vector<>::size_type x{5};
Is there any way this can be done?
I'm quite new to C and these days I have been playing around with a linked list. I managed to make a working version using pointers, ad only for the sake of learning I was trying to do the same thing just passing the "Object reference" Here is the method that apparently doesn't work..
Code:
struct Node addNode(struct Node head){
int value;
struct Node *n;
printf("Please enter the value
");
scanf("%d", &value);
[Code]...
when I return the function i have something like: head=addNode(head)
Unfortunately it does not work the way I aspect. I suppose that there is something I have left out..
Code:
like n->next=&head
// passing the address of the head at the next pointer of the struct
head =*n
//copy the values of the new node to the old head..
There must be something wrong with this line.. return head; What have I done wrong?
I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?
Code:
#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}
[code]....
Code:
returnType operatorOperatorSymbol (parameterList); // syntax for operator overloading
Class Fraction
public:
Fraction operator-(const Fraction& f1){
Fraction r;
return r;
}
Is this even syntactically correct? It gives me errors. Im just trying to compile it without errors. I think the function makes sense since its returning a type Class
understand the below program.
Why I'm getting output y is 6.000000
Code:
#include <stdio.h>
int square(double a);
int main()
[Code].....
I am new to c++ and trying to learn. for instance. i have a struct and method.I am trying to learn what i can do with the method if i define the return type as struct type.
struct S {
int age;
string name;
};
S method() {
//what i can do in here. with the Struct. I mean can i reach members of the struct. etc
}
I am wondering why return type for an assignment operator cant be a void or int? Cant I write assignment operator for student class like this as we do nothing with returned value?
Student {
char name[20];
int marks;
public:
student(char*name,int marks)
[code].....
In my main I have a do/while loop, which is to iterate around the method calculateImportance while i is not equal to the numNodes.
do
{
int i;
int numNodes = Test.count();
for(i=0; i<=numNodes; i++)
[Code]....
I have been messing around with the data type node, hence why it is there are present, but, that doesn't seem to the answer
This is the function declaration in the header file.
node CalculateImportance();
i'm getting a new error is a wrong return type.
WebAPI.cs
namespace DarkAPP___WForm.Libs {
class WebAPI {
System.Net.WebClient wc = new System.Net.WebClient();
public WebAPI() {
[code].....
/*
* symboltable.c
*/
#include "symboltable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/utlist.h"
[Code] ....
I want to give a try to "restrict" type-qualifier. I have looked for examples on google and I see the format is used: "int * restrict p";
However when I write this I get an error: "p is undeclared"
I am using GNU GCC Compiler with Code::Blocks.
How can I solve this problem. Is it possible me to adapt the compiler to C99 standarts, if I can, will it solve my problem?