C++ :: Function Pointers Inside Array
Apr 6, 2014
Ive recently got into function pointers, i find that they can be quite handy for making your program very 'dynamic'.
However, the syntax is very confusing for what i want to do
This is what i want to do
I want to hold function pointers inside an array, but this array is dynamically allocated ( malloc, realloc, etc )
This is the current syntax ive come up with, but i dont think it is correct
void ( **drawFunc ) ( void*, SDL_Surface* );
View 2 Replies
ADVERTISEMENT
Apr 16, 2014
I have a problem with pointer array, i passed a 2d array to a function but then i dont know how to make operations on it !!!
#include <iostream>
using namespace std;
int fun_name (int * arr) {
for (int i = 0;i< ??? ;i++) // how to compare while i don't know the size of array!!
[Code] ....
View 2 Replies
View Related
Mar 7, 2014
I am new to C++ and am trying to modify an array inside of a function.
It is of my understanding that you cannot return an array from a function?
Would writing it to a data file and then retrieving it be a solution to this problem?
View 2 Replies
View Related
Jul 24, 2013
im tasked with creating a linear search script using functions on a 10 element array. the elements are to be supplied by the user as is the search target.
I understand how to create the array and gather that information from the user as well as howto set a variable for "target", this is what im calling it. Those two parts are simple enough.
I am not fully understanding the calling of an array in a function as a pointer. i semi understand the use of a pointer and howto call a normal pointer in a function. i also understand that an array is nothing more then a "special" pointer with a set of consecutive address blocks for the size of the array.
My first user defined function is simple enough
Code:
ReadArray(int A[], int size){
int i;
printf("Please enter %d integer numbers separated by spaces:
", size);
for (i = 0; i < size; i++)
scanf("%d", &A[i]);
}
Sso nothing out of the ordinary there. that should be a standard for loop and use of scanf, sadly prof has not covered ssanf or any of the other options so i am stuck using scanf for now. maybe someday down the line in a other program or after this course ill get the chance to learn about better options for gathering data from the user.
I am confused as to my next function:
Code:
void SearchArray(int A[], int target, int size);
I've not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?
If i call *A do i then use something like this for my search:
Code:
for (*A = 0; *A < size; *A++)
if (*A < target) or use A[] insteadA?
Code:
for (i = 0; i < size; i++)
if (A[i] = target)
View 6 Replies
View Related
Jan 29, 2015
I basically have some code that lets users register callbacks into a callback table at a specified index. There is one element in this table for each event that can trigger a callback. I basically do something like this:
In a header file
Code:
typedef struct _GMclient
{
GMcommunicator gcomm;
GMclient_callback callback_table[(int)MAX_CALLBACKS];
}GMclient;
typedef int (*GMclient_callback)(GMclient*, void*, void*);
Then I allow them to set the callback with a function that basically ends up doing this (in a .c file that includes the previous mentioned .h file):
Code:
void
GMclient_register_callback(GMclient *client,
GMclient_callback fxn,
int index)
[Code] ....
Then later when I need to invoke that callback, I pull it out of the table
Code: GMclient_callback *fxn = client->callback_table[CMD_INDEX];
However, I get compilation errors:
Code: src/gmanager_client.c:43:6:
error: a label can only be part of a statement and a declaration is not a statement...
View 4 Replies
View Related
Aug 4, 2013
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){
[Code]...
View 3 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
Dec 13, 2013
I would like to know if this code is correct.
#include <iostream>
#include <string>
int say_one(const std::string &s) {
std::clog << s << ": One!
[Code] .....
View 7 Replies
View Related
Nov 21, 2014
I have a a group of text files that are used as input into a program. Another very similar program needs the same data in a different input format. I am writing a program that will read in each line of data and parse it. I have successfully written the program in main() such that it will read until the end of file. However, I am trying to re-write the program such that the algorithm is in an external function and will read one line at a time and then pass all of the character strings to the main program. The program will not compile and I am positive it has to do with an incorrect use of arrays in passing the variable "token". I am attaching a copy of the main program and the function.
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
void Line_Parse(std::ifstream&,std::ostream&,const char* token);
int main(int argc, const char * argv[]) {
[Code] .....
View 2 Replies
View Related
May 13, 2013
I'm trying to create an array of function pointers and then assign compartilbe functions to them, so I can just call *pf[0](xxx);
The functions are all of the type
void func01(unsigned char*, int, int)
how would I create an array of function pointers and assign the address of the functions to them? So I could call them like
ptrToFunction[i](charBuffer, 10, 20);
I've read a bit on line and I thought I could do it but so far I've failed.
It seems trivial and I feel I'm close but close isn't good enough.
I'd like to assign the fuction addresses like this:
for (int i=0; i<10; i++)
if (i==1)
ptrToFunction[i]=func01;
if (i==2)
ptrToFunction[i]=func02;
etc.
The actual logic is somewhat different than this but this close.
View 4 Replies
View Related
May 12, 2013
The premise of this assignment is to create a menu driven, array of functions calculator. This is the code I have thus far (I haven't finished all of my comments yet, I've been to focused on clearing errors).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*prototypes*/
void sum (int a, int b);
void dif (int a, int b);
void pro (int a, int b);
void quo (int a, int b);
void printMenu();
[Code] .....
I am using Visual Studio as a compiler, and I have the following errors:
error C2144: syntax error : 'int' should be preceded by ')'
error C2660: 'sum' : function does not take 0 arguments
error C2059: syntax error : ')'
error C2144: syntax error : 'int' should be preceded by ')'
[Code] .....
I can only assume that I am making simple mistakes.
View 4 Replies
View Related
May 24, 2013
I am having following pointer variable declaration
Code: Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};
Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.
View 2 Replies
View Related
Nov 11, 2013
I am suppose to find voltage by multiplying a resistance array and a current array by using a function and pointers. This is what I have so far:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iomanip>
[Code] ....
View 1 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{
[Code]....
then i want to call this template function in another ordinary function written in a seperate cpp file
these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions
View 4 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types.
i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;
[code].....
then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.
View 1 Replies
View Related
Dec 4, 2014
I keep on getting error message while trying to pass a function inside a for_each loop.. I have a vector and i used for_each loop to go through the rows in that vector, Now I need a function to do something
Example this is what I am trying to achieve
for_each (label.begin(), label.end(), addToTemporaryVector());
void DataPartitioning::addToTemporaryVector(){
cout<<"sucess";
}
But I get an error message saying: error: invalid use of void expression
View 2 Replies
View Related
May 1, 2014
How would I generate nodes inside a function for example I have a head node
Node * head = new Node;
Now I want to generate nodes for as long as the for loop is running
void CreateList(Node * h) {
for(int i = 0; i < 5; i++) {
Node * n1 = new Node;
head -> next = n1;
} }
Something like this but it should keep adding nodes to the list and change the head each time so how would I do this...
View 1 Replies
View Related
Jan 1, 2013
i usually use this method for accesing functions in executables, the code is executed from a DLL (always works, except when the function are inside of a class, even tho is public):
.h:
typedef int (*pgObjViewportClose) (OBJECTSTRUCT* gObj);
extern pgObjViewportClose gObjViewportClose;
.cpp
pgObjViewportClose gObjViewportClose = (pgObjViewportClose) 0x04F1940;
That works, but i can't get it to work if the accesing function is inside of a class, i get Unhandled Exception while trying to access a function inside a class, is there a way to do it?.
View 1 Replies
View Related
Aug 25, 2014
I want to have a function pointer inside a typedef struct but I get a segmentation fault when I run my code. Here's my code:
#include <stdio.h>
typedef struct Foo {
void (*bar)(int);
} Foo;
void func(int x) {
printf("display: %d
[Code] ....
View 2 Replies
View Related
Mar 3, 2013
Is this standard-compliant code?
int f() {
class C {
public:
int mf() const {return 1;}
};
C c;
return c.mf();
}
View 1 Replies
View Related
May 15, 2013
In the following code:
#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}
Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?
View 19 Replies
View Related
Jul 20, 2013
Say you had:
class Foo{
public:
//...
void funky();
[Code] .....
Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?
View 3 Replies
View Related
May 18, 2013
This is a program I developed in which we had to define a class named BOOK with the data members and member functions as shown in the program..We have to:
(i) Make the user enter the values in the array BOOK.
(ii) Display the details that the user entered.
(iii) Search for a book from the array upon its Bno and display its details.
(iv) Search for a book from the array upon its Bname and display its details.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class BOOK {
private:
int Bno;
char Bname[20];
[Code] .....
But while running it the compiler gives the errors as:
Line 43 to 48: Illegal character '' (0x5c)
Line 69: Undefined symbol 'Display'
Line 88: 'BOOK::Bno' is not accessible.
Line 89:'BOOK::Bname' is not accessible.
Line 90:'BOOK::Author' is not accesible.
Line 91:'BOOK::Price' is not accesible.
Line 108:'BOOK::Bno' is not accessible.
Line 109:'BOOK::Bname' is not accessible.
Line 110:'BOOK::Author' is not accesible.
Line 111:'BOOK::Price' is not accesible.
from 43 to 48..the line feed was also used at many other places but there it was not given as an error so why here?
Line 69: I defined the Display() function outside the class since it contained control structures, so what's the error then?
About the lines the rest of the error( the "not accessible" ones) I know these data members are not accessible because they are in private visibility mode. But then how to make them accessible? (Without putting them in public because it was a part of the question to create the data members in private).
View 1 Replies
View Related
Nov 7, 2013
I've written an Array class to create 1d,2d and 3d array and it works fine for every test : example of the constructor of the array class for 2d case:
Array::Array( int xSize, int ySize ) {
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}
It works fine , but when i need to use this constructor inside of other constructor, i get the "no matching function error" ,
part of my code:
class StaggeredGrid {
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}
[Code] .....
View 2 Replies
View Related
Aug 4, 2013
So, I have this code:
...
MakeTexture((char*)ilGetData(), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));
...
And it compiles fine and, supposedly, works fine too, but when I try to put a break point inside the MakeTexture fuction gdb just goes crazy, it freezes and starts alocating memory until it reaches like 30+ mbs, and after that codeblocks freezes and I have to terminate the gdb process to return everything back to normal.
Now, another weird thing is that this only happens if I pass (char*)ilGetData(), if I pass something like NULL to the function, this doesn't happen.
Oh, also, the MakeTexture function is this:
int MakeTexture(char *pxData, unsigned w, unsigned h)
{
return 0;
}
View 2 Replies
View Related
Jun 7, 2013
Alright, so I have a code that's not giving me errors, but it doesn't seem to retain what I put into an array. Not sure If I'm missing something...
Code:
#include <stdio.h>
int main (void)
{
const char *pointer;
const char alphabet[] = "ABCDEFG";
pointer = &alphabet[5];
printf("pointing to %c of the alphabet
", pointer);
return 0;
}
Trying to get my pointer to return the letter in the [5] spot or "F". Not receiving any errors when compiling, but I seem to get different answers every time I run it.
View 5 Replies
View Related