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
ADVERTISEMENT
Jun 1, 2013
How to pass the char something[8][8] for example to function void ( char pass.....)???
View 7 Replies
View Related
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
Sep 25, 2014
#include <iostream>
using namespace std;
void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer
int main () {
int x = 5;
[Code] .....
SOLUTION:
#include <iostream>
using namespace std;
//Purpose to create a function that returns a pointer to a pointer
int** myfunc(int**);
int main () {
int x = 5;
[Code] ....
View 3 Replies
View Related
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Jun 13, 2013
void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {
[Code]...
Why it is not working? What would be a proper way to pass function pointer as a template parameter?
View 6 Replies
View Related
Jan 3, 2015
I am trying to create a callback system for input events in my game engine.
Here is a cut down version of the EventManager.h file
#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };
[Code].....
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{
void p_pressed(void){
//pause
}
[Code].....
View 6 Replies
View Related
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
View Related
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
Feb 4, 2014
Why does this code doesnt work?
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string {
char* ptr;
[code] ....
View 1 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
May 25, 2013
I get a run time error saying something about memory locations when I run this program.How can I make this program work using pointers?
//this program converst miles to km
#include<iostream>
#include "std_lib_facilities.h"
[Code]....
View 1 Replies
View Related
Feb 9, 2015
//program to form a header file
/* using pass by reference in pointer */
#include <iostream>
#include<math.h>
[Code].....
View 14 Replies
View Related
Jan 24, 2013
What is the difference in pass by pointer and pass by reference? As per my understanding, there is no difference much.If a function accepts pointer, then NULL check can be performed.other than this i'm not able to see any big difference..
View 3 Replies
View Related
Mar 14, 2013
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
View 2 Replies
View Related
Jul 30, 2014
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
View 6 Replies
View Related
Mar 1, 2014
Why do most C examples pass a double pointer when manipulating linkedlists? Why can not we just pass a single pointer to the struct?I think using an external reference accessor for a linked list would be a more appropriate solution, yes or no?
View 1 Replies
View Related
Jul 24, 2013
class A (abstract)
class B : A
class C {
void add ( A(&*?) a )
std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible
}
upper situation. What is the best way to pass add an object of class B to C?
with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); }
and
int main() {
C c;
c.add( new B() );
}
This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.
View 10 Replies
View Related
Nov 30, 2014
The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes.
#include <iostream>
class base;
class node {
private:
node** data;
public:
[Code] .....
View 3 Replies
View Related
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
Feb 12, 2014
I'm trying to pass 2 arrays into a void funtion, and return values to one function.
this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.
#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };
[code]...
The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
View 6 Replies
View Related
Jun 27, 2014
I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)
My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.
Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file
[Code] ....
View 1 Replies
View Related
Dec 26, 2014
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')
[Code] .....
View 3 Replies
View Related
Feb 3, 2013
I thought we needed to allocate memory before assigning a value to a char* and also that we needed to use functions like strcpy() to copy something into it. Then how come this works and does not crash?
Code:
#include <iostream>
using namespace std;
int main()
{
char * buf;
buf = "Hello";
cout << buf << endl;
buf = "World!!!!!!!!";
cout << buf << endl;
return 0;
}
View 3 Replies
View Related
Aug 31, 2013
I like to use a Pointer to char array. And then I would like to do a Pointer Arithmetic by incrementing the Pointer. Finally I would like to see the Addresses of the Pointer to each of the char Array Elements. I had created a program below, but I am not getting any Addresses from my Pointer.
#include <iostream>
using namespace std;
int main () {
int ArraySize;
char ch[]= "This is a Char Pointer";
char* iPtr = ch;
[Code] ....
View 3 Replies
View Related
May 4, 2013
#include <iostream>
using namespace std;
int replace(char *ptr, char c1, char c2);
int replace(char *ptr, char c1, char c2){
int count = 0;
int i = 0;
[Code] ......
Segmentation fault (core dumped)
View 5 Replies
View Related