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++){
The snippet below (or similar) compiles and runs OK but I am using Visual Studio C++ compiler. Are the lines where .nameFirst and .nameLast assigned kosher in ANSI C?
Also I am concerned about the memory allocation for these string constants. Does the runtime system put them on the heap? It doesn't seem that they are really constants since they are not defined before runtime.
I coded a program that takes some strings and lexicographically orders the strings and its substrings. I have used dynamic memory allocation technique and its working fine for all strings without consecutive same alphabets.I use a list in which a string is placed in its exact position by moving the others right.
Trying to append a comma to a string. Getting "Segmentation Error" on Solaris when the function is entered the second time.
Code:
// Appends a comma to the given string void appendComma(char* instring) { if (instring == NULL) { instring = realloc(NULL, strlen(",")); strcpy(instring,",");
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"
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'm writing a program involving a theoretical hotel. Most of the code is already written, but the part I'm having trouble with involves the very beginning of the code.
The program is designed to read in from an input file the hotel's ID number, the types of rooms it has, the Room Numbers of each room, the base rate for each room, and the rate of additional charge per person.
The code demonstrates inheritance for my Object-Oriented Programming class, as each type of room will inherit from generic class Room, but I can't seem to figure out how to make the double-pointer for dynamic allocation work.
The code is below.
Hotel.h Code: class Hotel{ int hotelID; static const int MAX_SIZE = 101; Room **rPoint;
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.
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?
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 */
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?
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?
I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:
where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:
double* Rin = new double[length];
and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.
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).
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:
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?
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.
What is the efficiency of the two assignments (line 1 and 2), i.e. (function calls, number of copies made, etc), also the Big O notation. I know there are function calls for retrieving the size of each string in order to produce a new buffer for the concatenated string...any difference between line 1 and 2 in terms of efficiency?
String s("Hello"); String t("There"); 1. s = s + t; 2. s += t;
Ok, so I'm writing this code and when I build it keeps saying cannot implicitely convert type int to string even though I declared my variables as string. Why is it giving me this error?
private static string Repair() { string result=""; string beep; string spin; Console.WriteLine("Does your computer beep on startup?:(y,n)");