C :: How To Return As A Pointer To A String
Aug 1, 2014How to return as a pointer to a string?
View 5 RepliesHow to return as a pointer to a string?
View 5 RepliesHere is what's up:
struct Square {int number, myClass* myclass};
int main() {
vector<myClass> classes;
myClass unrelated;
classes.push_back(unrelated);
Square newClass = {3, &classes.at(0)};
.
.
.
myClass is a class I have. Now, in the class, I have a function what_value and I need to get the classes.at(0) from the pointer to it in another function. But the problem is, how can I do it? I'm completely stumped, here's what I thought of:
newClass.*myclass.what_value();
And it I get an error from the compiler. Basically, how can I do this in another function with a pointer:
classes.at(0).what_value();
Here is the part of my code that I need to return two values. I am working on a roulette program and I need to return the choice and the number they are betting on. How can I use a pointer to achieve this?
Code:
int makeBet(char choice, int num){
printf("
What type of bet would you like to place? ");
printf("
Type n for number.
Type e for even/odd.
Type d for dozen.
[Code] ....
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?
Ok here is the code
//assuming that all the directives have been included here
void ptrPointer(vector<string>* const p, int n);
int main() {
vector<string> inventory;
vector.push_back("sword");
vector.push_back("knife");
[Code] .....
Well...my question is how come the last statement or cout statement to be accurate won't work and display? Since we can do something like this below?
string text = "some text";
string *p = &text;
cout << p;
I passed the address of the vector to the function. And then display the address. It is a pretty common operation. But it just won't work. But then we do something simple similar to that of the vector. But it works.
I need understanding the logic behind this function. The function is supposed to "Return a pointer to the character at the index given" . For example, what exactly are we declaring at "(char * const c, int index)"? where does "index" come from or equal to in the body of the function?
Code:
1
2
3
4
5
6
7
8
char * GetValueAtIndex(char * const c, int index)
{
int i = 0;
char *p = c;
while (i++ != index)
p++;
return p;
}
/*
* symboltable.c
*/
#include "symboltable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/utlist.h"
[Code] ....
question from chapter 11, qn 8 modern C programming by king
Write the following function: Code: int *find_largest(int a[], int n); When passed an array a of length n, the function will return a pointer to the array's largest element
Code:
#include <stdio.h>
int *find_largest(int a[], int n)
{
int i, x;
x = 0;
[Code].....
have traced the code line by line and it prints correctly up to line 31. However, when the program exits (line 35) , it goes to some other screen with very complex code and my output disappears.
Just to clarify: If I just run the code as is, there is no output. However, when I trace the code line by line, the output appears, but then disappears as the program exits My input was 1 2 3 4 5 6
I'm trying to write a function that returns a pointer to a dynamically allocated array. Here's my code:
#include <iostream>
using namespace std;
void IndexArray(int, int);
int main(){
int *arr, n;
[Code] ....
When I try running the program, I get the error
"Unable to start program 'D:C++FilesdynamicArraySolReleasedynamicArray.exe'. The system cannot find the file specified."
I'm honestly not sure if the issue is my program, or something with C++. At the moment, I cannot debug any of my programs or else I get the same exact error. I basically need to release everything without debugging it. I last used C++ about a year ago and I'm finally back in school, and so trying to get back into it. I use Microsoft Visual C++ 2010.
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?
I have a function that I want to exit gracefully when an "error" occurs in an input file. My function declaration is:
Code: BSTnode *buildTree(FILE *fp)
The few lines that are causing the problems are:
Code: if(regcomp(®ex, to_find, REG_EXTENDED | REG_NEWLINE) != 0) {
fprintf(stderr, "Failed to compile regex '%s'
", to_find);
return EXIT_FAILURE;
}
I know that if I just use "return" by itself the warning goes away but fails to exit when the error occurs. I also believe this may not be the correct use of stderr. But I need the program to exit when an error has occurred.
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 Relatedi 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...
heres the function:
string ToString ( const char * format, ... )
{
char buffer[256];
[Code]....
these function do the same of the sprintf() function. but instead we use a variable for add the result, i want to return the result. when i use it:
string f;
f=ToString("hello world");
gives me several errors:
"error: crosses initialization of 'std::string f'"
"error: jump to case label [-fpermissive]"
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.
The code below is supposed to fill, show, and revalue property. The fill function is supposed to return a pointer that creates a range of property values. The show function is supposed to show the property values entered and the revalued property values. I think part of the problem is the returned pointer from the fill function. Once that is cleared up, I think I will find more problems.
Code:
#include <iostream>
const int Max = 5;
// function prototypes
double fill_array(double ar[], int limit);
void show_array(double * begin, double * end);
[Code] .....
I need to make a copy of a string that is defined by char *full and copy it into a different pointer defined by char *duplicate. I have written code to do this however it will not work and i cannot figure it out my code is as follows:
char *duplicate = (char *)malloc(strlen(full) + 1);
strcpy(duplicate, full); /*Make second version of full*/
char *Ptr = strtok(duplicate, " "); /*Split duplicate up*/
I have a full program written but i know this is where the problem is because i have used printf statements to see where the program fails. I get no errors and it compiles successfully but it hits this point of the program and it just stops and windows automatically shuts down the program.
char *full is pointing to:
"To be, or not to be? That is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,"
I need to duplicate the string because i need to use strtok but i will need the original string later on so i need an unaltered version.
I need a function to return a string..i need to pass input as "a,b,c,a,c,d,e" function should return out put as "a,b,c,d,e".
View 3 Replies View RelatedI am trying to code a function which will read a file on system and return its content back as string. Code is below.
Code:
char * readtxt(){
FILE * fptr;
char c;
static char txt[30];
[Code]....
I suppose txt variable is pointer. But I need to return the file content as string so the function structure should look like
Code:
returnType function(){
return "File Contents as String";
}
string str = "sfsdfsd"
and i want to add like str[3]+str[4]
but it return some integer value
I am trying to code a function which will read a file on system and return its content back as string. Code is below.
char * readtxt(){
FILE * fptr;
char c;
static char txt[30];
int len=0;
fptr = fopen("C:UsersTestDesktopDev est.txt", "r");
[Code] ....
I suppose txt variable is pointer. But I need to return the file content as string so the function structure should look like
returnType function(){
return "File Contents as String";
}
How can I achieve this ?
PHP Code:
int processString(xxx)
{
//do something
return non-null terminated string length
}
To make the function work better we change its internals
PHP Code:
int processString(xxx)
{
//do something
return null terminated string length
}
This phenomenon can be observed by use of mutibyte to wide char conversions, string copy , concatenation functions of MS etc ....
Should I minus one in the return value of the second function to match both versions ?
I need a function to return a string
I need to pass input as "a,b,c,a,c,d,e"
function should return out put as
"a,b,c,d,e"
I try to make dll and install it under "Component Services". I tried to make method that return string but when i test it i don't get any result :
Code:
STDMETHODIMP CSimpleChat::CellMe(BSTR name, BSTR** helloMessage) {
CString temp = _T("Hi ");
temp += name;
temp += ", welcome to the simple chat server!";
BSTR str = temp.AllocSysString();
*helloMessage = &str;
return S_OK;
}
I'm trying to return a string to then call it on main function.
Code:
const char* coiso(int chave){
char str [50];
sprintf(str,"%d",chave);
char txt[50] = ".txt";
strcat(str,txt);
return str;
}
int main () {
const char* info = coiso(8);
printf("%s",info);
}
If I do a printf("%s",str) in coiso function it works but the following code doesn't work.
I've to use recursion function that return if the string has a same letters:
for example: - fff = true
- fFf = false
I did this but the return answer is always: NOT!
bool iSameLetters(char str[SIZE]) {
if(str[0]='