Questions tagged [char-pointer]

Character Pointer is a data type which holds the address of a primitive charter type variable.

Character pointer can point to a memory address which holds a character value in the form of ASCII Code. A static character array also decays into a char pointer. A Char pointer can be allocated memory dynamically using this syntax.

char *char_ptr = new char ;

It can also be allocated a whole char array and it points to the first index of that dynamic array.

char *char_ptr = new char [10] ;

Typically, as with other pointers, a char pointer takes 4 bytes of memory in the system. Its properties match those of other pointer types with slightly different behavior. The compound operation of assignment and output are allowed on char pointer. If a char pointer points to a cstring, these operations are possible on it which are not legal on any other pointer type.

char *char_ptr = "hello world" ;
cout << *char_ptr ;
171 questions
0
votes
0 answers

Why I can't I directly duplicate a certain character of a C string with strcpy and instead must use an auxiliary string?

I wrote the following program to add a '*' character after every vowel in a C string #include #include using namespace std; int main() { char s[99]; cin.get(s, 99); char aux[99]; for (int i = 0; i < strlen(s); i++) …
Lastrevio2
  • 51
  • 6
0
votes
2 answers

Cannot directly convert number to hex null-terminated string, has to convert to std::string then use .c_str()

I've tried to convert an integer to a hex null-terminated (or "C-style") string but I cannot use it with printf or my custom log function. It only works if I convert it to an std::string then use .c_str() when passing it as a parameter, which…
Credix
  • 3
  • 1
0
votes
2 answers

Can a string literal be concatenated with a char*?

I know that in C adjacent string literals are concatenated. I want to know, are adjacent string literals concatenated with char*s? The reason I'm asking this question is because I want to pass a concatenation of two strings to perror(), and one of…
Galaxy
  • 2,363
  • 2
  • 25
  • 59
0
votes
1 answer

C: Adding integer to char pointer(address)

I am getting from a function a pointer to the (empty at the start) data of a block. This pointer is: char* data; And my job is to insert different types of data in this block. I know only the data type (string/int/float) and their length in…
Nick Stavr
  • 13
  • 4
0
votes
1 answer

valgrind invalid read of size 1 - occuring during comparisons

I have created a program that works fine but when I run it through valgrind with these flags: -g -std=gnu11 -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused…
kalle konsida
  • 323
  • 2
  • 5
  • 12
0
votes
4 answers

C programming: scanf for char pointer not working

I have a program which takes in the user input for classroom and time. The classroom input is store inside a char* classroom, and the time is store inside a int time. However, when I run the program, it stops at when I press enter after I have type…
Daaenerys
  • 11
  • 1
  • 5
0
votes
2 answers

Char pointer puzzle

I'm new to c and attempting socket programming. I have a question about pointers. Say I have a setup like this: int main() { ... // some code int numbytes = receive(i); } int receive(int num) { ... // some code msgLength = ... //…
Asool
  • 13,031
  • 7
  • 35
  • 49
0
votes
3 answers

C - Copy char array that contains '\0' to another char array, eliminating '\0'

Is there any C library function that copies a char array (that contains some '\0' characters) to another char array, without copying the '\0'? For example, "he\0ll\0o" should be copied as "hello".
Yoghi
  • 49
  • 8
0
votes
1 answer

Passing multiple string to a function from c++ code to c code throw an error

I have a struct as below struct st { std::string name; std ::string refer; int number; std::string path; }; Have created array of above structure as struct st var[5]={{"rick1","ross1",1,"demo1"}, {…
leuage
  • 566
  • 3
  • 17
0
votes
5 answers

SIGSEGV on modifying char * element

In the below program, #include #include int main(){ const char *str1 = "abc"; char *str2 = (char *)malloc(sizeof(char)*4); str2= "def"; str2[1]='s'; printf("str2 is %s", str2); } Debugger : (gdb) ptype str1 …
overexchange
  • 15,768
  • 30
  • 152
  • 347
0
votes
2 answers

pointer-to-char-array assignments in c/c++

In the past i've been using Visual Studio 2010/2013/2015 and this syntax was possible : char* szString = "This works!"; I've decided to step on and change my lifestyle of coding towards Linux, as I have installed g++ and have SlickEdit as my…
Noam Rodrik
  • 552
  • 2
  • 16
0
votes
1 answer

Bubble sort to char pointers

i am trying to make a program that inserts to an 'char** arr' a char pointer and then sort the char pointers with strcmp but for some reason its just don't work, the code i added is the sorting part of the entire code. do { flag = 0; …
Ofek Ezon
  • 29
  • 1
  • 5
0
votes
1 answer

Memory exception while trying to append char pointer to a fixed string

I have a requirement where I have to get the RFID RSSI value which is an int and convert it to a char pointer and append to it. Below is how I did it. char *epcBytes = (char *)tag_operation_report->tag.epc.bytes; int rssiString =…
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
0
votes
6 answers

C++ tolower/toupper char pointer

Do you guys know why the following code crash during the runtime? char* word; word = new char[20]; word = "HeLlo"; for (auto it = word; it != NULL; it++){ *it = (char) tolower(*it); I'm trying to lowercase a char* (string). I'm using…
mask
  • 539
  • 1
  • 5
  • 18
0
votes
2 answers

Difference between char, char* inside Linked Lists

I created a linked list that hold int and char type data. A function adds the data to the list and the other prints it out. When i only print the int type i get no problems but when i try to also print the char type the program crashes. So it has to…
Mimic01
  • 107
  • 1
  • 12