C++ :: Returning A Pointer To 2D Array From A Function
Dec 10, 2014
I know how to pass a 2-D array to a function. The prototype for that is void f(int (*p)[2]) assuming the array is of integers and there are 2 columns in it.
However, if I wanted the same function to return a pointer to a 2-D array, what would be the prototype?
View 2 Replies
ADVERTISEMENT
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
Mar 5, 2013
I have a program that is trying to find all factors of an integer given. It needs to be done in a recursion function. Right now i have code similar to just getting the prime factors of a integer.
unsigned int * find_factors_using_recursion(unsigned int x ) {
unsigned int * factor = new unsigned int[];//do i put x in here ?
for(unsigned int i = 2; i < x; ++i) {
if(x % i == 0) {
find_factors_using_recursion(x / i);
*factor = (factor[i] = i);
} }
return factor;
delete [] factor;
}
When i cout the *factor = (factor[i] = i) it gives me the prime numbers of the integer passed into the function but when I return the pointer it only returns one of the prime numbers. I'm new to c++, how to return pointers from functions that would be great with an example to go with it.
View 10 Replies
View Related
Apr 25, 2014
I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.
Here is a snippet:
Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}
Visual studio is saying that the return type doesn't match.
View 3 Replies
View Related
Mar 31, 2013
I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.
int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;
[Code]....
View 1 Replies
View Related
Jun 17, 2013
I am trying to neaten my code by putting them in different cpp files but in one function an array is changed:
int function(int array2[100][9])
{
for (int i =0; i < 100; i++)
[Code]....
View 9 Replies
View Related
Mar 2, 2015
I'm having trouble returning a char array by a function, here's the code. The problem is the 'reverse' function, the purpose of the function is to send two char arrays, 'newline' containing the char array, reverse it and place it in the 'rev' char array then output it back in main, however the output remains blank so I assume there must be something wrong with the reverse function.
Code:
#include <stdio.h>
#define MAXLINE 10
int fgetline(char line[], int maxline);
void copy(char to[], char from[]);
void reverse(char forw[], char rev[], int arrsize);
[Code] .....
View 1 Replies
View Related
Feb 14, 2014
I'm trying to return a short int in a function..This is my function signature :
short int* StartRecord(int seconds)
this is my array :
short int waveIn[NUMPTS];
I'm trying to get the data into an array :
short int arr [] = StartRecord(3);
getting this error: Error2error C2440: 'initializing' : cannot convert from 'short *' to 'short []'
View 1 Replies
View Related
Apr 1, 2013
I have been trying to make a function that compares all the values of the array and if they are all equal will return a value to print true. The problem I am having is that regardless of what values I enter the function is always returning true. Any way to tell the program o check all the values in one command instead I put them each,
Code:
#include<stdio.h>
#include<stdlib.h>
int compare(int arrayA[], int arrayB[]);
[Code]....
View 2 Replies
View Related
Sep 18, 2014
How can I return a dynamically allocated 2d array from a function? Do I use like this:
int main(){
char **array;
array=func();
} char ** func(){
char** ptr=new char[5]; //five words
ptr[0]=new char[size of word1];
*ptr[0]=word1
........
return ptr;
}
View 8 Replies
View Related
Jul 27, 2014
This a very simple program I created because I dont understand how do this. My goal is to be able to use the pointer *s5 throughout the program. For example I would to like to call other functions and pass that pointer through the function. I understand the dynamic allocation and pointers for the most part but Im confused here because the "new char[20]" variable will die after the function and I dont want it to.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
void testArray ( char *s5 );
int main ( int argc, char *argv[] )
[Code] .....
Also does strlen count the null terminator?
View 1 Replies
View Related
May 15, 2014
I want to return a char array to the main() function, but its returning garbage value.
#include<stdio.h>
//#include<conio.h>
#include<string.h>
char* strtrmm();
int main() {
char str1[100],c1[100];
[Code] .....
View 5 Replies
View Related
Apr 24, 2013
I hear all the time about the rule of not returning a local pointer, but I'm not exactly sure what that insinuates. Would this example be classified as violating that rule?
class Foo {
public:
Bar* ReturnBar() {
Bar* temp = &bars.at(0);
return temp;
}
private:
vector<Bar> bars;
};
View 3 Replies
View Related
Mar 24, 2015
I am trying to return a char pointer so that i can re use it again. I am writing a vigenere function that takes a message, a key and an initialization vector where it performs the encryption, prints out the encrypted message and returns the encrypted message. I print out the process step by step and everything works, however i pass the answer and print it out again and only the first letter gets changed. I put my code below and my output right after that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
void decrypt(char *to_encrypt, char *key, char* pct);
enum flag{encryption = 1, decryption = 0};
[code].....
View 3 Replies
View Related
May 29, 2014
I have a class which looks like the following:
Code:
class Gadgets {
public:
Data* GetData() const {
return _data.data();
}
const Data * GetDataConst() const {
return _data.data();
}
private:
QSharedPointer<Data> _data;
}
You may ask why i have the second GetData() method that return a pointer to my private member, which will then expose my private member externally. I provide this function just for the case where user can do the following
Code:
Gadgets * myGadgets = new Gadgets();
myGadgets->GetData()->ModifyData(.....);
My question pertains to the use of QSharedPointer of data(), where in the doc, it says
T * QSharedPointer::data () const
Returns the value of the pointer referenced by this object.
Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.
Whereas, if i were to use
Code:
Data* data as my private member class instead of QSharedPointer;
I will not need to worry if the caller do as follow
Code:
Data * data = new Data();
Data *data2 = myGadgets.GetData();
data2 = data;
delete data;
View 3 Replies
View Related
Aug 18, 2014
Should i never return a pointer to a local variable. i have seen the following code in the standard text book.
Code:
struct node *add_to_list(struct node *list, int n)
{
struct node *new_node;
new_node = malloc(sizeof(struct node));
// some code here
return new_node;
}
new_node is a local pointer then why is he returning the new_node? Is it wrong or am i failing to understand something?
View 2 Replies
View Related
Jun 22, 2014
So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.
Find my code below:
#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {
[Code] .....
View 1 Replies
View Related
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
Apr 24, 2014
I can clearly understand how to pass a dynamic array to a function and modified within the function..
My aim is to get back the array initialized as I am trying on the next sample of code..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define N3//number of bodies
void initialise( double *M[], double *X[], double *y[])
[code]....
View 14 Replies
View Related
Feb 2, 2015
I am trying to do this.
if (isInteger(&(tokens[i][j])) == true || isDouble(&(tokens[i][j])) == true)
Instead of sending the address of the element at row I and column j, it send the address of the entire row I. What is wrong?
View 8 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
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
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
Aug 21, 2014
In case the question was not very clear. Say I have the following code:
int sum(int * x, size_t N){
int sx;
/* returns sum of elements in x */
return sx;
[Code] ....
I'm curious to know whether there is a way that ensures a function does not modify the memory pointed to by a pointer.
View 9 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 20, 2015
I am making a game commonly know as the Hangman using C++.
Now I am trying to add a man in it like this:
0
|/
|
/
Now the problem i am facing is that i am using a check that if a function returns the value 0 "return 0" it means the guess is wrong and it will not update the man but if it returns any value there will be a function called which will update the man.
I just wanna know that how i am going to use the check, the kind of thing that i am trying to use is, in general words "if(function returns a value) then update the man"
int main() {
return match;
}
How are we going to use it in check that if int main is returning 'match' in the check...
View 3 Replies
View Related