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
5
votes
5 answers

Why can't I assign an array variable directly to another array variable with the '=' operator?

Why does the following assignment not work? I would like a low-level explanation if possible. Also, here's the compiler error I get: incompatible types in assignment of 'char*' to 'char [20]' class UCSDStudent { char name[20]; public: …
Kacy Raye
  • 1,312
  • 1
  • 11
  • 14
4
votes
3 answers

Determining memory allocation size for string input in C (scanf)

I want to get a string input from a user in C. I know how to use scanf to get string inputs. I know how to use malloc and realloc to allocate space for a variable. The problem is when the user enters a string, I don't necessarily know what size that…
gcr
  • 443
  • 2
  • 5
  • 14
4
votes
1 answer

copy array of ansichar to array of char delphi 10.2

as we know from Delphi 2007 and below the strings are not Unicode Char is 1 byte AnsiChar 1 byte from Delphi 2009 and above the strings are Unicode Char is 2 byte AnsiChar 1 byte when i convert my Delphi code from Delphi6 to Delphi 10.2( unicode…
4
votes
3 answers

Constant character pointer using unique_ptr

Let's say that I have a vector input and I need to pass this to a function that takes a const char** argument. My thought had been to use a unique_ptr like this: const auto output = make_unique(size(input)); But I can't seem to turn…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
4
votes
6 answers

Use of pointers on strings

I am really confused about the use of pointers on strings. It feels like they obey different rules. Consider the following code char *ptr = "apple";// perfectly valid here not when declaring afterwards like next ptr = "apple"; // shouldn't it be…
Gaurav Pant
  • 962
  • 10
  • 30
4
votes
3 answers

Why can compiler only implicitly convert char * to std::string in some cases

These work: struct WithString { WithString(std::string){}; }; void takeString(std::string){} //implicit conversions: takeString("hello"); WithString("hello"); But this does not: WithString makeWithString() { return "hello";} // error: no…
johnbakers
  • 24,158
  • 24
  • 130
  • 258
4
votes
3 answers

Generate Unique Values

I want to create a C program to generate numbers from 0 to 999999, keeping in mind that the number generated should not have any digits that are repetitive within it. For example, "123" is an acceptable value but not "121" as the '1' is repeated. I…
4
votes
2 answers

What is the difference between pointer to 2D char array and pointer to 2D int array?

#include int main() { char str[3][15] = {"Pointer to","char","program"}; char (*pt)[15] = str; // statement A char *p = (char *)str; // statement B printf("%s\n",p[3]); // statement C - Seg…
ssg
  • 247
  • 2
  • 15
4
votes
2 answers

C++ error on char pointer

I want to initialize a char* string, inside a struct. this is the struct: typedef struct __String { char* data; // C-string data, unsigned* copyRef; // number of copy reference, delete only when the copyRef is 0 bool …
Vargan
  • 1,277
  • 1
  • 11
  • 35
3
votes
2 answers

Is it safe to compare const char* with == in C/C++?

Let's say I have a struct that keeps track of a type using a const char*: struct Foo { const char* type; } Suppose I only ever assign this value using a string literal throughout my program: Foo bar; bar.type = "TypeA"; Foo baz; baz.type =…
David Callanan
  • 5,601
  • 7
  • 63
  • 105
3
votes
2 answers

Strange outputs from appended char* in C++

I was just writing simple code and then I saw something strange. The code is supposed to append a string to another string. The output from the new appended string outputs not only the correct appended string, but it also adds every time four time…
3
votes
2 answers

C# get string/characters value from c++ function which return char pointer

I've a DLL written in C++. A function of this DLL is like the following code: C++ code: char _H *GetPalette() { -------Functions body -------Functions body return pPaletteString; } Now I want to…
Shahriar Hasan Sayeed
  • 5,387
  • 1
  • 16
  • 12
3
votes
1 answer

C++: Unexplainable behavior with cout and pointer to char

After printing a pointer to an int, I print a pointer to a char: #include using namespace std; int main() { int i; cout << "&i: " << &i << endl; char q = 'q'; cout << "&q: " << &q << endl; return 0; } I get the…
Daniel Gilbert
  • 133
  • 1
  • 7
3
votes
1 answer

Converting Structure to char* pointer in C

I am having a structure: struct K { char a[10]; char b[10]; }; I wish to convert this structure to a char* pointer and print the value on Uart. Uart takes char* pointer as input. My main function looks like: void main() { struct K x= {…
Pradeep Ch
  • 103
  • 3
  • 11
3
votes
2 answers

Prefer Iterators Over Pointers?

This question is a bump of a question that had a comment here but was deleted as part of the bump. For those of you who can't see deleted posts, the comment was on my use of const char*s instead of string::const_iterators in this answer: "Iterators…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
2
3
11 12