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).
I have two char* that have the same data in (hypothetically).
std::vector<char*> Buff; Buff = Split(Line, '.'); char* A = "data", B; B = Buff.at(0)
Where Split is a function that I made to split a string (Line in this case) into a char* vector, this string contains a line from a file. Line is char* too. The weird problem is when Buff data stored in its 0 position is given to B... because B is equal to A (hypothetically) but when this is compared to do certain functions they doesn't match!
Here an example:
std::vector<char*> Buff; Buff = Split(Line, '.'); char* A = "map", B; B = Buff.at(0) // Buff.at(0) should be "map" and is apparently "map"
[Code].....
NOTE: I didn't use switch to compare Cmd because I want it separately for easier debugging.
Is there something wrong with my codes?? or what happened here with those hex values before the string in my variables?
i have been trying to compare a date format from SYSTEMTIME and a date from a text file(string).But its not working. I tried to change both to string(using osstringstream),char* and int(using sscanf) to do the comparison but with no luck. its pretty simple all i want to do is get the current system date and compare it with the date from the text file. Below is my code:
char szcurrentDate[MAX_PATH] = ""; char szdate_time[MAX_PATH]; SYSTEMTIME st; GetLocalTime (&st); GetDateFormat(LOCALE_USER_DEFAULT,NULL,&st,"yyyy-M-d ",szcurrentDate,MAX_PATH); //current system date //std::ostringstream mm;
[code].....
note : i tried displaying just szcurrentDate and szdate_time they show the date exactly the same. in string,char* or int formats.
I thought we needed to allocate memory before assigning a value to a char* and also that we needed to use functions like strcpy() to copy something into it. Then how come this works and does not crash?
I like to use a Pointer to char array. And then I would like to do a Pointer Arithmetic by incrementing the Pointer. Finally I would like to see the Addresses of the Pointer to each of the char Array Elements. I had created a program below, but I am not getting any Addresses from my Pointer.
#include <iostream> using namespace std; int main () { int ArraySize; char ch[]= "This is a Char Pointer"; char* iPtr = ch;
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.
Here is my code. I am combining two words and sorting the merge word in alphbetical order. The compiler giving me warning error
Program:12:4: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat] Program:14:4: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat]
Alright, so I have a code that's not giving me errors, but it doesn't seem to retain what I put into an array. Not sure If I'm missing something...
Code: #include <stdio.h> int main (void) { const char *pointer; const char alphabet[] = "ABCDEFG";
pointer = &alphabet[5]; printf("pointing to %c of the alphabet ", pointer); return 0; }
Trying to get my pointer to return the letter in the [5] spot or "F". Not receiving any errors when compiling, but I seem to get different answers every time I run it.
I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like
char* charptr; and then calling myfunct(charptr);
and then inside the function i would set charptr equal to another char ptr, simply like
charptr = anothercharptr;
But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.
I solved this issue by calling the function like
myfunct(&charptr);
and declaring the function like myfunct(char**);
and then I had to dereference the charptr in the function when assigning it to another ptr, so *charptr = anothercharptr;
It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.
What is the difference between the value of the char* charptr written as either charptr and &charptr?
I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:
Returns: BASE_OK RS232 data acquisition success BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure
I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:
# include "Baseboard.h" int main () { Baseboard _Baseboard; // Class name is Baseboard char *msg ;
[Code] ......
The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?
Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .
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?
I came across some code and it's not clear why it is casting an unsigned char * to another pointer type only to free it right after. Here are the relevant structures:
As you can see, _Edge_Message has a *msg field, but in the function below, they cast it to the other two structure types inside the case blocks of the switch statement only to free it. What is the point or advantage of doing this?
Code: void _edje_message_free(Edje_Message *em) { if (em->msg) { int i; switch (em->type) {
I have function that looks like this myfoo(char* Name) Now i want to compare this name to another one . But the another name is a pointer . This my code :
I understand why I think. When I am passing the reference to the array possion it is outputting everything up to the next /0. So my question is how do I stop it?
I dont have much choice as to how the output wants it:
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.