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
2
votes
1 answer

C Guarantees Re: Pointers to Void and Character Types, Typecasting

My best-effort reading of the C specification (C99, primarily) makes me think that it is valid to cast (or implicitly convert, where void *'s implicit conversion behavior applies), between any of these types: void *, char *, signed char *, unsigned…
mtraceur
  • 3,254
  • 24
  • 33
2
votes
1 answer

Valgrind: Invalid write of size 1

I'm trying to create a block of memory on heap and copy some data to it using memcpy, but memcpy gives a segmentation fault. I feel I have allocated enough memory, but there seems to be an invalid write. I'm not able to reason why it happens. The…
user1124236
  • 453
  • 2
  • 7
  • 17
2
votes
2 answers

Pass char pointer/array to a function

I am trying to understand char pointer in C more but one thing gets me. Supposed I would like to pass a char pointer into a function and change the value that pointer represents. A example as followed: int Foo (char *(&Msg1), char* Msg2, char*…
Y. Chang
  • 595
  • 1
  • 5
  • 18
2
votes
3 answers

store value in char array pointed by char pointer

I have a char pointer. char *ch; char arr[100]; Now I want to store the value of the char array ch is pointing to in another array arr. I want to store the value pointed to by ch permanently, so that any other operation performed on ch will not…
sara
  • 95
  • 5
  • 14
2
votes
3 answers

char pointers, char arrays, and strings in the context of a function call

What is the difference between the line that does not compile and the line that does compile? The line that does not compile gives this warning: deprecated conversion from string constant to 'char*' Also, I'm aware casting (char *) on the string…
Kacy Raye
  • 1,312
  • 1
  • 11
  • 14
2
votes
3 answers

C++ error: cannot convert ‘char (*)[63]’ to ‘char*’ in initialization

I seem to be more of a noob in C++ than I originally thought. As far as my knowledge of C/C++ goes, this should work. I define a character array then try to assign a pointer to the beginning... What am I doing wrong? // Create character array char…
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
1
vote
2 answers

how literals in c are stored when defining with both character array declaration and character pointer declaration?

I read that while using character pointer declaration first the string literal is getting stored in static storage and then pointer to it is returned. but is same happening while using literals in char array? here I know "instatic" gets stored in…
Aka
  • 33
  • 5
1
vote
1 answer

C++20 changes reading into char arrays with `operator>>` - How to fix this?

EDIT: To summarize from the comments (before I close the topic): the issue has been discussed here previously: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0487r1.html The resolution was that the committee was aware they break code…
1
vote
1 answer

C: String Variable Loses Value After Returning From Void Function

hope you are well. I recently started learning ADT (Abstract Data Type) in college, and I have an assignment that states the following: Complete ADTDate adding the following primitive function: void dayWeekStr(DatePtr date,char *buffer)// places…
1
vote
1 answer

Pointer printing first char of string

#include using namespace std; char* input(char **S); int main() { char **S = new char *; char *K = input(S); //cout << K << endl; cout << *K << endl; } char* input(char **S) { cout << "Enter string: "; cin >>…
Ron
  • 23
  • 3
1
vote
1 answer

Value of const char* returns empty after construction

Casting this Vector3 into a const char* has worked in implicit and explicit conversions, but hasn't when attempting to convert it at construction time. Only then does const char* 'v3pchar' return blank. Any thoughts? The full code is below.…
joseph taylor
  • 123
  • 12
1
vote
2 answers

Why *c++ increasing pointer value but not the ascii of first Char?

#include int main() { char *c = "test"; while (*c != '\0') { printf("%c", *c); *c++; // or c++ both produced same result. ie test } return 0; } As *c++ should increase value and c++ should increase…
user2681304
  • 129
  • 6
1
vote
1 answer

Array of pointers to array of strings, can only access first element of array

Hi im trying to access strings stored in 2 different arrays using an array of pointers using the code: typedef char (*p)[2][20]; //pointer to array of strings ///create dictionaries char names[2][20] = {"jack","john"}; char…
JAck
  • 15
  • 2
1
vote
2 answers

How to import an exported char* from C++ dll in C# project

I'm trying to open a char* that's being exported from a Dll in my C# project. Since the codes are too much, I'll write a short code that is similar to the code that I'm working on. C++: __declspec(dllexport) typedef struct Kid { char* _name; …
1
vote
3 answers

C - Last element of char* array

For below sample code; char* g_commands[]={ "abcd", NULL }; int main() { g_commands[1] = "efg"; char ** tmp = &g_commands[0]; for(; *tmp != NULL; tmp++){ printf("%s", *tmp); } return 0; } since…
Ugur KAYA
  • 167
  • 3
  • 14