C++ :: Return Struct Pointer From A Void Function And Print

Mar 17, 2013

i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code

struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}

Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...

View 3 Replies


ADVERTISEMENT

C++ :: Pass 2 Arrays Into Void Function And Return Values To One Function?

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

C++ :: Void Triangle - Print Class Function

Sep 1, 2013

//Point.cpp
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;

Point::Point() { //Initialise the point to the origin.

[Code] ....

void Triangle::print() { //print out the Triangle with the format "( (x1, y1), (x2, y2), (x3, y3) )"

How do I accomplish this? When i test with cout << _point1.print(), there's an error:

[Error] no match for 'operator<<' in 'std::cout << ((Triangle*)this)->Triangle::_point1.Point::print()'

View 4 Replies View Related

C++ :: Void Pointer Argument In A Function?

Jun 11, 2014

Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?

Code:
#include <stdio.h>
int compareid(void* info, int value); // ansi declaration
int compareid(void* info, int value)

[Code] .....

View 5 Replies View Related

C++ ::  Why Every First Function Of Each File Get Error - Multiple Definition Of Void Pointer

May 6, 2014

I declared all functions in header file, such as:

bool readCase();

bool meshing();
bool readMesh();

bool calculateFlowfield();
bool readFlowfield();

bool calculateEvaporation();

And then I define them in separated .cpp files, each .cpp file include the header, but I got multiple definition error, why?

Even the int main() function, which only decalred and defined once got this error, why?

View 14 Replies View Related

C :: How To Print Struct From Another Function

Oct 31, 2013

how to print struct from another function? let's say I have code this way

Code:

struct student{
int id // simple example
};
main(){
modify_data(); // calls the function.
}

[code]....

View 9 Replies View Related

C++ :: Accessing Inside Structure Via Struct Pointer To Struct Pointer

Jun 5, 2012

"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);

[Code] ....

These codes give these errors:

error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'

These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"

I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.

View 3 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 :: Return Errno From Open Function And Print It To Screen

Mar 22, 2013

i am programming Cli/Ser ,so part of the program is to open file to download it from client side,but i want to return the global value Errorno to print it to screen , and how to use strerror() func ??? i write this "This is part of server code" :

Code:

fd= open("abc.txt",O_RDONLY);
//error handilng in open file
if(fd<0)
{

[Code].....

View 3 Replies View Related

C :: Function Pointer Inside A Typedef Struct

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

C :: Making Function That Will Return Pointer To Long Variable?

Feb 7, 2014

I am making a function that will return a pointer to a long long variable. For example, I have the next variable prototype: Code: long long funcName(long long x, int s); I want to change the return value, and the first parameter to pointers to long long.

View 4 Replies View Related

C++ :: How To Convert Void Pointer To Int / Double / String Pointer

Mar 7, 2013

I have a function:

const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;

[Code] .....

But this causes an error of: "crosses initialization of int*intPtr"

What's the correct way of implementing this?

View 1 Replies View Related

C++ :: How To Use Function With Pointer Parameter To Print Array

Jul 20, 2013

The printArray function should take in the dynamically created array and the size of the array as parameters. It should print out the contents of the array.

#include <iostream>
#include <string>
using namespace std;

[Code].....

My problem is that how to write the code to print the array using pointers. I've been stuck for awhile trying to figure it out.

View 2 Replies View Related

C :: Void And Return Statement

Jan 11, 2014

If i declare a function as a void function. But for testing purpose if i use a return statement in the function definition. i have tested and found that the function does not return and executes the entire function. How does the function not return even if a return statement is available? Does the compiler removes this return statement or how it is?

View 2 Replies View Related

C/C++ :: Const Pointer Pass By Reference In Print Function

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

C :: Dereferencing Of Void Pointer

Apr 14, 2014

I could understand void pointers I created the following program:

Code:
#include <stdio.h>
#include <string.h>
int main(void) {

char word[] = "Zero";
int number = 0;
void *ptr = NULL;

[Code] .....

The program works fine, however i really want to fully understand what is going on with the dereferencing of the void pointer, for example: With the following code:

Code:
ptr = &number;
*((int *)ptr) = 1;

Why can't you just do:

Code:
ptr = &number;
*(int *)ptr = 1;

And again with this code, (i'm guessing it's becuase its a pointer to a pointer?):

Code:
ptr = &word;
strcpy(ptr,"One");

View 2 Replies View Related

C :: Printing From Void Pointer

May 26, 2014

Code:
int main() {
List* newList= lst_new();
names* nama;
char* data;
int x=1;

[Code] ....

I cant seem to be able to print a string.. the functions lst_next() lst_first() return void*.

View 9 Replies View Related

C++ :: Void Pointer As A Parameter

Mar 14, 2013

I want to have a function that has a pointer to an int/double/string so I thought I'd use a void pointer like so:

int someFnc(int a, int b, const void *key){
// take care of converting key into appropriate type in here
}

And when I want to use this function I'd like to be able to do something like this:

main{
...
int myKey;
someFnc(1,2,myKey);
]

But I get a compiler error telling me:

invalid conversion from 'int' to 'const void' -[fpermissive]

Do I need to convert myKey into a void pointer before passing it as an argument?

Why does passing myKey like this work?

someFnc(1,2,&myKey);

View 1 Replies View Related

C/C++ :: How To Use Void Pointer Functions

Sep 27, 2014

int (*cInts)(void*,void*);
cInts= &compareInts;

int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);

View 5 Replies View Related

C++ :: Reference Pointer - Void Exchange

Mar 15, 2012

#include "vehicle.h"
...
void exchange(vehicle *&v1, vehicle *&v2) {
vehicle *tmp = v2;
v2=v1;
v1=tmp;
}

Is it right?

How about: void exchange(vehicle *v1, vehicle *v2){...}

What is the difference between *&v1 and *v1 ?

View 4 Replies View Related

C :: Sorted List - Why Value Of Void Pointer Change

Feb 16, 2014

I have a the following in a header file.

Code:

struct SortedList{
void * data;
struct SortedList * next;
struct SortedList * previous;
int (*compareFunc)(void *, void *);
void (*destructor)(void *);

[Code] ....

In a .c file, I implemented the SLCreate function.

Code:
SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df){
struct SortedList item;
item.data = NULL;
item.next = (struct SortedList *) malloc(sizeof(struct SortedList));

[Code] ....

In main.c, I try to do the following:

Code:
SortedListPtr list = SLCreate(&compareInts, &destroy);

A bunch other code that does not alter list or it's contents at all.

struct SortedList item = (*list);
void * data = item.data;
if (data != NULL) {
printf(Why did data become not null???
"); }

How come my variable data became not null anymore when I haven't altered it at all....

View 2 Replies View Related

C++ :: Cannot Cast From Void Pointer - Returns Always Error C2440

Apr 25, 2013

I having a problem which I'm not able to resovle. I try to dereference a void pointer but I always get a C2440 error. It says: 'static_cast':void* cannot be converted in wqueue<T>. I tried different cast ways but I always get the same error. As far as I found out I should get the error if I try to dereference without cast but in my case I cast before and still get that error.

void *srumbler (void *arg) {
wqueue<workclas*> m_queue= static_cast<wqueue<workclass*>>(arg);
return NULL;
}

The according type wqueue in the header file:

template <typename T> class wqueue {
list<T> m_queue;
pthread_mutex_t m_mutex;
pthread_cond_t m_condv;

[Code] .....

View 3 Replies View Related

C++ :: Add Variable Address To Void Pointer Inside Of Class?

Dec 1, 2013

How can I add the variable adress to a void pointer inside of a class?

class variant2 {
private:
void *Vvariant=NULL;
public:
template<typename b>
variant & operator = (b *adress)

[Code] ....

if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&')
for the moment i just need put the address to Variant pointer. but i receive several errors .

View 4 Replies View Related

C :: Print Pascal Triangle And Stores It In A Pointer To A Pointer

Nov 27, 2013

I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.

Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");

[Code] ....

View 4 Replies View Related

C++ :: Template Return Struct Fields

Dec 2, 2014

is it possible to define as template the following get functions.

class Config {
public:
enum { NO_ID = 999 };
struct ValueType {
bool a;
size_t b;
std::time_t c;

[code]....

View 6 Replies View Related

C :: Unable To Return Struct Node Without Using Pointers

Jul 20, 2014

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?

View 2 Replies View Related







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