C :: Dereference A Pointer In A Function

Feb 25, 2015

Code:
void dereference(int* a, int* b)
{
a=b;
}

int main(int argc, char **argv)

[Code] ....

Why isn't f and d the same after calling "dereference(f,d);"

View 8 Replies


ADVERTISEMENT

C++ :: Cannot Dereference Variable In A Set

Jun 29, 2014

I get errors C2100 (illegal indirection) and C2088 ("<<": illegal for class) while trying to use the myPrint function (irrespectively of whether the set for printing contains pointers or not):

template <class T = std::string>
void myPrint(const std::set<T>& mySet) {
std::cout << "Size of vector is " << mySet.size() << ", Pointers: ";
bool pp = false; std::string str = "no";

[Code] ....

The errors point to the line "if (pp) { std::cout << *(*i) << ","; }", but I cannot figure out what's wrong with it...

View 4 Replies View Related

C++ :: Cannot Dereference Iterator At The End Of List?

Apr 8, 2014

Why this code works
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 4 elements

but this not work?
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 3 elements

This is remove method:
Persona Elenco:: remove(int pos){
list<Persona> ::iterator iter=l.begin();
for(int i=0 ;i<pos;i++){
iter++;
}
return *(l.erase(iter)); //erase ritorna un iterator
}

View 4 Replies View Related

C++ :: How To Make Sure Dereference To Vector Is Valid

Oct 6, 2014

I have this piece of code in parts of my path finding algorithm

for( int head; head < q.size(); ++ head ){
walk& w = q[head];

// do manything with w
if( some_condition ) q.push_back( walk( w.x + 1, w.y, head ) );
}

However I notice that sometimes w is cannot be dereferenced. It can but it throws junk number at me. Perhaps the vector is changing it size and move the whole array to a different location. Is there anyway to make sure that w is always valid ?

I just want to use w because of shorter typing and cleaner look not because of performance. I also refrain from using macro.

View 8 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

Mar 14, 2013

I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.

Code:

position = hash->(*funcHash)(idNmbr);

The function will return an int, which is what position is a type of. When I compile this code,

I get the error: error: expected identifier before ( token.

Is my syntax wrong? I'm not sure what would be throwing this error.

View 3 Replies View Related

C++ :: System For Managing Radio Station - Crashes When Tries To Dereference The Iterator

Jul 28, 2013

My assignment is to write a system for managing a radio station. The code is composed of four classes:

Song: each song has a name, a composer and a singer, and has a few segments: INTRO,VERSE,CHORUS,TRANSITION, while each represents a different length string of chars.
Playlist: a multiset of songs, and a pointer to a RadioStatistics instance (see below).
RadioStation: a set of Songs (will represent the station database), and a list of Playlists, each playlist holds a few songs from the database.
RadioStatistics: can only be instantiate once, this object gather statistics; it has two maps: one that counts how many times a song was played, and second that counts how many times each singer was played. (the key is the song/singer name, and the value is the counter).

The RadioStation has a constant that defines a limit to how many times a song is allowed to be played. whenever a song reaches this limit (meaning, it was played too much), the program needs to skip to the next song in the database.

so, I run this test from main, and the program crashes (or more accuratly get stuck, since the console stays open and the program keeps working until I stop it).

I made a few changes and run the debugger a few times, and was able to focus on the problem.

Song.h:
Code:
#ifndef SONG_H_
#define SONG_H_
#include<iostream>
#include<string>

[Code] ....

I ran a step by step debugger, and found out the problem lays with line 90 in RadioManager.cpp, when the while loop runs its fourth iteration. It crashes when it tries to dereference the iterator, while it points to the fourth playlist in the list.

And here's some more weird stuff: when I comment out line 73 in main.cpp - it works perfectly fine! (line 73 in particular! commenting out any other line in main.cpp didn't worked around the bug!)

View 13 Replies View Related

C++ :: Calling Defined Function Pointer From Another Pointer To Class Object?

Aug 19, 2014

I am attempting to implement function pointers and I am having a bit of a problem.

See the code example below; what I want to be able to do is call a function pointer from another pointer.

I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.

class MainObject;
class SecondaryObject;
class SecondaryObject {
public:

[Code]....

View 10 Replies View Related

C/C++ :: How To Get Formation From Pointer Function To Main Function

Feb 13, 2014

int example (int [], int, *int,*int,*int,*int);
int main () {
My code will be here
example (int array[], int size, &a,&b,&c,&d); // Like this??? I try it didnt work

[Code] ....

View 2 Replies View Related

C :: Using Pointer With A Function

Mar 6, 2015

I tried to use pointer with a function but I guess I am missing out on something

Code:
#include<stdio.h>
int add(int *a,int *b){
int *c,*d;
*c = *c+1;
*d = *d-1;

[Code] ....

And this one too

Code:
#include<stdio.h>
void add(int *a){
int *c;
c = c+1;

[Code] ....

View 7 Replies View Related

C++ ::  What Does Pointer Before Function Mean

Apr 1, 2014

I was wondering what magic does a * pointer before function actually do? Today our programming teacher asked us to look into it and explain it in the next class!

#include<iostream>
using namespace std;
int *binary(int []);

[Code].....

View 2 Replies View Related

C :: Assign A Pointer To A Function?

Jan 24, 2013

How can we assign a pointer to a function? const char* function_name(), here what exactly does the pointer point to?

View 4 Replies View Related

C :: Returning Pointer From Function

Nov 21, 2014

As the title says, i'm using a function which returns a pointer to a struct:

the struct is the following:

Code:
typedef struct POINT
{
uint16_t x;
uint16_t y;
}

Coordinate; the function i'm using:

Code:
Coordinate * Read_XTP2046(void)
{static Coordinate screen;
//calculations to determine the coordinates
screen.x=(temp[1]+temp[2])/2;
screen.y=(temp[0]+temp[2])/2;
// and so on...
return &screen;}

The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.

In my main program i would do the following:

Code:
Coordinate cor;
cor = Read_XTP2046();

This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.

View 8 Replies View Related

C :: Initialization Of Function Pointer

Apr 28, 2013

I would like to initialize an arry containing function pointers with adresses of functions that have a variable number of arguments.

Below the code that works in principle. I would however get rid of the warning message during compilation pointing to the initialzation of the funtion pointers in the array. How do I need to cast the pointers to the functions ?

Code:
gcc func_ptr_init.c
func_ptr_init.c:28: warning: initialization from incompatible pointer type
func_ptr_init.c:32: warning: initialization from incompatible pointer type

Code:
#include<stdio.h>
unsigned char func1_ptr(unsigned int* if_desc, int* result_code) {
*if_desc = 1;
*result_code = 1;
return(0);

[Code] ....

View 8 Replies View Related

C++ :: How To Return A Pointer From Function

May 1, 2013

I am trying to return a pointer from a method. Below is a sample of my code.

CSubnode * CTest::GetSubNode() {
return m_psubnode;//this is declared in CTest as CSunbnode * m_psubnode
}
//in another class
m_subnode = m_ptest->GetSubNode(); //m_subnode is declared as a pointer

Is this the correct why to return a pointer?

View 2 Replies View Related

C++ :: Setting The Value Of A Pointer From Function

Feb 20, 2013

I declared a pointer in main with value 0, so I want to change its value so that it points to other variable from a function, I guess the function creates a copy of my pointer that's why whatever I do within function doesn't change the real direction of the pointer in main. I've been trying something like this:

#include <stdio.h>
void redirectionate(char *str, char *ptrCopy);
int main()
{

[Code]....

View 7 Replies View Related

C++ :: What Is The Actual Use Of Function Pointer

Dec 27, 2013

two things I did'nt get till now

Q->1 what is the actual use of function pointer ?

Q->2 what is use of passing function as an argument to another function ?

View 2 Replies View Related

C/C++ :: Pointer As Function Arguments

Dec 26, 2014

How this code work bcoz when pointer variable assigned in called function and how different values get as resultant output, ans 2 97 for below code. How the code wil execute so that i can validate ans

#include <stdio.h>
int main() {
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);

[Code] ....

View 6 Replies View Related

C++ :: Pointer To Member Function?

Jun 6, 2012

I am trying to use "remove_if" with a predicate function inside a class. The code intends to remove the grid cells which an agent cannot move into (from among all possible cells).

Code:
void classname::function1()
{
vector<MoorePoint> neighbors;
....

[Code]....

That code would work if it was not in a class and the predicate was not a member function. However, now I receive long error messages which I guess refer to incompatibility of remove_if template with the predicate parameter (one error includes : error C2064: term does not evaluate to a function taking 1 arguments).

View 2 Replies View Related

C++ :: How To Properly Use Pointer To A Particular Function

Apr 4, 2013

#include <iostream>
using namespace std;
int function(int a,int b) {
return a + b;
} bool function2(int a,int b)

[Code] .....

View 3 Replies View Related

C++ :: Pass Object As A Pointer In Function

Apr 12, 2013

i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.

main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"

[Code].....

View 7 Replies View Related

C++ :: How To Call A Thread By A Function Pointer

Nov 24, 2014

How to call a thread by a function pointer?

I get "error C2059: syntax error : '(' " on the indicated line.

Code:
#include <windows.h>#include <iostream>
using namespace std;
DWORD WINAPI Thread() {
cout << "ok" << endl;
return 0;

[Code] ....

View 9 Replies View Related

C :: Passing Structure Pointer To A Function

Aug 24, 2014

I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.

[Code] .....

Also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50

View 2 Replies View Related

C :: Passing Array Of Pointer To A Function

Sep 7, 2013

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt);

[Code] ....

I am trying to pass the value of fun_tknzd to str_tknzd

View 4 Replies View Related

C++ :: Passing Pointer Into Template Function

Oct 14, 2014

I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. how to make the function accept the pointer.

//main
int main()
{
srand(unsigned(time(NULL)));
int size;
int *list;
int *listCopy;

[code].....

View 4 Replies View Related

C++ :: Pass A Pointer Into Function - Value Of Char?

Jan 2, 2014

I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like

char* charptr;
and then calling
myfunct(charptr);

and then inside the function i would set charptr equal to another char ptr, simply like

charptr = anothercharptr;

But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.

I solved this issue by calling the function like

myfunct(&charptr);

and declaring the function like
myfunct(char**);

and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;

It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.

What is the difference between the value of the char* charptr written as either charptr and &charptr?

View 1 Replies View Related

C++ :: Using API Function That Has Char Pointer As Argument

Feb 5, 2014

I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:

BASEBOARD_ERROR_KIND ZMP zrc :: :: :: Baseboard GetRS232Data (char * msg )

RS232 data acquisition.

Argument:
[Out] msg Address of the acquired data.

Returns:
BASE_OK RS232 data acquisition success
BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure

I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:

# include "Baseboard.h"
int main () {
Baseboard _Baseboard; // Class name is Baseboard
char *msg ;

[Code] ......

The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?

Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved