C :: Changing Value Of A String Through A Pointer

Jun 11, 2013

Basically, I have a pointer to a C string:

Code: char **objectData and a C string:

Code: char temp[350]; I need to assign the CONTENTS of the temp to the CONTENTS to which objectData points.

Code: *objectData = temp; //this changes the pointer. When temp is deleted, objectData points to some rubbish

**objectData = *temp; //this doesn't seem to be doing anything - the string to which objectData points does not change.

View 3 Replies


ADVERTISEMENT

C++ :: Changing The Value Of A String?

Dec 17, 2013

If I was to input for example 'x' into my program, how could I change that to something like 'HuS581' every time that specific character was inputted?

View 2 Replies View Related

C :: Finding And Changing String With Another String

Mar 6, 2015

I have a question about this function.

Code:

char a[4] = {"aaa"};
strstr(a, "bb");

When I do this after function copies bb to the array it puts '' ? I mean last for array would be 'b' 'b' 'a' '' or 'b' 'b' '' ''. I am trying to learn the basics of searching a string, finding and changing them with another string.

View 14 Replies View Related

C++ :: Changing String To Bool?

Jan 21, 2015

I want to change string to bool but I'm having trouble doing this.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int check_Pythag(int DIMA, int DIMB, int DIMC) {

[Code] ....

View 2 Replies View Related

C++ :: How To Convert String To Double Without Changing Actual Data

Jun 3, 2013

I have to convert string to double. i'm using "atof" function to achieve same.

I have string as "0.0409434228722337" and i'm converting with "atof" But i'm getting double value as "0.040943422872233702". Why it adds 02 additionally at the end?

More example :

"0.0409434228722337" converts to "0.040943422872233702"
"0.067187778121134" converts to "0.067187778121133995"

Is there any other possibility to convert string to double without changing data ?

View 5 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/C++ :: Copying A String From A Pointer To New Pointer

Mar 4, 2015

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.

View 9 Replies View Related

C++ :: Changing Array Of Characters To A String Array?

Apr 7, 2013

I have an array titled: char TypeOfSong[arraySize] where the array size is 15. I am reading data from a file into this array and the characters can be either 'C', 'D', 'E', or 'R'. Each of these characters stands for a word (sting) and when I output the array, I need the strings to show up, not the characters. I have been reading online and in my book but I can only find information on turning one array with the same characters into a string. How would I go about changing this character array with different characters into a sting?

The characters stand for:

C = Country
D = Dance Party
E = Elevator
R = Rock

View 5 Replies View Related

C :: String Allocation To String Pointer

Feb 10, 2014

I am trying to read the string from user and the allocate it to the another string which is ptr string but not successful . Do I have to use any dynamic memory allocation here?

Code:
int main(){
char test[5];
char *strng, *base;
int i;
base=strng;
for(i=0; i<4; i++){

[Code]....

View 8 Replies View Related

C :: How To Return As A Pointer To A String

Aug 1, 2014

How to return as a pointer to a string?

View 5 Replies View Related

C :: Pointer To String In Functions

Feb 10, 2015

I made this example code to illustrate my question:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Right(char* In, char* Out, int Position){
Out=&In[strlen(In)-Position];

[Code] ....

Well, I guess the code explains it all. I want b to be the last three characters of a using a function called "Right".

Doing exactly the same thing without the function involved works fine. I just let b point to the third last character of a.

Why does the function not work?

View 9 Replies View Related

C++ :: Binary Char Pointer Into String

Jun 12, 2013

Is there a more simple method to copy Buf into str? Buf is a binary string.

Code:
void function(string & str) {
int iWholeSize = 512;
char * Buf = new char[iWholeSize];
.... operations on Buf
string s(Buf, Buf + iWholeSize);
str = s;
}

View 5 Replies View Related

C :: Assign Pointer Of A String To Integer

Mar 22, 2014

I have this code:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[code]....

What I want is basically to assign to the *p the pointer of the string so that i could do the following printf(" print string %s",*p); so i dont know how to do that.

View 6 Replies View Related

C :: Entering Characters Into A String Pointer

Feb 8, 2013

1. The debugger shows that the characters are entered into the word pointer, and when a punctuation or space character is encountered, I terminate the string with a ''. But when I use puts to print the string, garbage is printed. Why is this?

2. Also, if I don't allocate memory for word the compiler gives a warning about it being used uninitialised. But I didn't allocate memory for the array of pointers(words), and the compiler didn't give any warnings. Whats the difference between a pointer and an array of pointers?

Code:

// wordcount.c - count words.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

[Code]....

View 7 Replies View Related

C :: Assigning A Pointer To A String Literal

Sep 13, 2013

In another forum, this example code fragment was stated as being an example of undefined behavior. My understanding is that a literal string exists from program start to program termination, so I don't see the issue, even though the literal string is probably in a different part of memory.

Code: /* ... */
const char *pstr = "example";
/* or even */
char *pstr = "example";
/* as long as no attempt is made to modify the data pointed to by pstr, */
/* unless pstr is later changed to point to a stack or heap based string */

View 11 Replies View Related

C++ :: Converting Char Pointer To String

Mar 5, 2013

Here's the code I'm working on:

string* arrayPush(string *array, char **toks){

if(array[sizeofHistory -1].empty()){
//find the next available element
for(int i=0; i < sizeofHistory; i++ ){

[Code] ....

toks is an array of pointers to strings. I need to assign a toks to array[i].

View 10 Replies View Related

C/C++ :: Pointer-to-char To String Literal?

Oct 6, 2014

how string literal that works with the cin object?

char * str = "This is a string constant";

Is the str stored the address of the first character of the string literal?

But some books just state that the pointer-to-char (char pointer) stores the address of the string literal". So just wonder how it is.

When it is used with cout, cout just treats it like a string and instead of printing the address, it just prints out all characters one by one until it reaches the terminated null character.

If this is the case, then I am just wondering how cin works with it? with a statement like this cin >> str; ?

Does the computer allocate enough memory for it? and then cin stores the first character into the first address and then advances to the next address and stores the next character?

View 1 Replies View Related

C++ :: C-style String Array And Pointer

Feb 9, 2014

I was told that if I define

char *cstrp;
char cstra[c];

then the cstrp can be treated as cstra, and so I can also use

cin>>cstrp;

but when I write the following program, I find it don't work, don't have clue

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char cstr[5];

[Code] ....

for cstr, it work exactly as what I expected, but for cstrp, no matter what I input, with a null terminator or not, I just got nothing printed. why? can we really use cstrp in that way or not? How to use it?

View 7 Replies View Related

C :: WAP To Remove Vowel String Using Pointer And Function

Jan 25, 2013

WAP to remove vowel string using Pointer and Function...

View 6 Replies View Related

C++ :: Unable To Compare A String With A Char Pointer

Jan 28, 2013

Ok, I'm having a few problems with strings, mostly string functions saying they're not able to compare a string with a char pointer.

int main()
{
int counter = 0;
int x, y, z;

[Code].....

My goal is to take in a command and store it in a string. Different commands have different amounts of information I need. One command is "new flight <flightnumber> <seats available>". First, I try to understand which command is being asked, using the first character to decide. Once I can assume which command is being attempted, I try to separate the string into smaller strings (words) using strtok. I then compare the words that should be constant (new and flight), to make sure the command is syntactically correct (which I use strcmp for). Then I'll go on to create a new flight, which is a class that takes in a char * and integer (which is why I need to convert a char * to integer).

View 2 Replies View Related

C++ :: Append Suffix To String With Pointer Arithmetic

Mar 1, 2013

Dynamic memory allocation and pointer arithmetic with char arrays.

The class was given to me in a very basic skeleton form with prototypes but no implementations, along with a test function to test my implementations. I CAN NOT use any C String functions in this assignment.

The part of the program which is troubling is the append function, which just appends a parameter string215 object to the end of the current string215 object.

// Add a suffix to the end of this string. Allocates and frees memory.
void string215::append(const string215 &suffix) {
char *output = new char[str_len(data)+suffix.length()+1];
for(int x = 0; x < str_len(data); x++) {
*output = *data;

[Code]...

This portion of the code is tested in the 13th test of the test function as shown here:

string215 str("testing");
...

// Test 13: test that append works in a simple case.
curr_test++;
string215 suffix("123");
str.append(suffix);
if (strcmp(str.c_str(), "testing123") != 0) {
cerr << "Test " << curr_test << " failed." << endl;
failed++;
}

Here is the description of the append class: Add the suffix to the end of this string. Allocates a new, larger, array; copies the old contents, followed by the suffix, to the new array; then frees the old array and updates the pointer to the new one.

My program aborts at the very end of the append function execution with the error message:

Debug Assertion Failed!

Program: [Source path]dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
...

Abort || Retry || Ignore

Here's a pastebin of the .cpp and .h file for this program

string215.cpp: [URL] ....
string215.h: [URL] .....

View 14 Replies View Related

C++ :: String Tokenization - Moving Pointer Ahead

Jul 5, 2014

I know there is strtok() to do the tokenization;but all strtok() examples I have seen only output one token at a time, moving the pointer ahead. Can't the tokens be saved somewhere?

View 6 Replies View Related

C++ :: Class Constructor With String Const Pointer As Parameter

Feb 19, 2013

I know my product.cpp constructor is wrong but how can I set my private variables.

//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H

[Code].....

View 2 Replies View Related

C++ :: Why Cannot Copy String Pointed By Pointer To Array Of Char

Feb 22, 2013

I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.

private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];

[Code].....

View 9 Replies View Related

C :: Changing The Value Of Something From A Function

Oct 30, 2013

I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int add(int ** TOP, int * stack);

int *stack = NULL;

[Code] ....

Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the address of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there?

View 7 Replies View Related

C++ :: Changing Const Int Value?

Nov 7, 2013

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

// cin.get() <-------------- used to let the user read the screen

// Function prototypes
void calcSales(const int [], const double [], double [], int);
void showOrder(const double [], const int [], int);

[Code] ....

How can i change the "const int NUM_PRODS = 12;" from saying id 1, id 2, id 3, etc. to custom product numbers?

View 1 Replies View Related







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