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

char *str; str="HELLO"; How does that work without allocating any memory for the string?

Code: #include int main() { char *str; char i = 'a'; str = &i; str = "Hello"; printf("%s, %c, %x, %x", str, i, str, &i); return 0; } I get this output: Hello, a, 403064,…
3
votes
2 answers

Why do I get a seg fault when I use ++ but not when I use '1 +'?

Please explain why I get the segfault using the ++ operator. What is the difference between explicitly adding 1 and using the ++ operator ? using namespace std; #include int main() { char* p = (char*) "hello"; cout << ++(*p) <<…
Kacy Raye
  • 1,312
  • 1
  • 11
  • 14
3
votes
2 answers

int8_t* and char*

Why does the statement const int8_t* cstr = "asdf"; gives error invalid conversion from ‘const char*’ to ‘const int8_t*’ Aren't int8_t* and char* same? Am I missing some subtle thing here?!
zeropoint
  • 235
  • 1
  • 4
  • 10
2
votes
4 answers

Is String Literal in C really not modifiable?

As far as I know, a string literal can't be modified for example: char* a = "abc"; a[0] = 'c'; That would not work since string literal is read-only. I can only modify it if: char a[] = "abc"; a[0] = 'c'; However, in this post, Parse $PATH…
HDHDHD
  • 31
  • 4
2
votes
2 answers

How does memory allocation work with char pointers(string literals, arrays)?

Currently reading K&R and just got stumbled across the char pointers. There's nothing about memory allocation when defining char pointers in the book rn, maybe it'll be explained later. But it just doesn't make sense so I'm seeking help :) 1 // No…
2
votes
2 answers

C++ Heap Corruption in pthread function realloc(): invalid next size

Hi I have a cross platform C++ app running on Fedora25 which is predictably crashing after a around a day of execution with an error realloc(): invalid next size. I have narrowed the issue down to on particular pthread which sends out periodic…
user3583535
  • 240
  • 2
  • 16
2
votes
1 answer

Convert into const(char)* from float in D

DrawText's first argument needs to be a const(char)* but i tried using to! for that and have failed :( yVel = to!(string)(player.vel.y); DrawText(yVel, player.pos.x, player.pos.y - 40, 20, RAYWHITE); How to properly convert from float to…
Soft Waffle
  • 356
  • 1
  • 12
2
votes
2 answers

Why do char arrays get lost when returning from a function in C++?

I know that, if we declare variables inside a function without allocating memory for them, they will be lost after the function finishes its job. The following code prints: (null) 5 char* getString() { char arr[] = "SomeText"; return arr; …
No N
  • 151
  • 2
  • 11
2
votes
3 answers

How to find the number of elements in char** array?

I have an string array in the form of char** I am struggling to find the length of that array: typedef struct _stringArray { int (*Length)(char**); char** (*Push)(char**, char*); char** (*Pop)(char**, char*); }StringArray; StringArray*…
bleepzter
  • 9,607
  • 11
  • 41
  • 64
2
votes
1 answer

Generic swap function using pointer to char in C

I don't understand so well how this code works: #include void gswap(void* ptra, void* ptrb, int size) { char temp; char *pa = (char*)ptra; char *pb = (char*)ptrb; for (int i = 0 ; i < size ; i++) { temp = pa[i]; pa[i] = pb[i]; …
sam0101
  • 373
  • 1
  • 4
  • 15
2
votes
5 answers

Why doesn't the compiler complain about adding char to char*?

Why c++ compiler doesn't complain for the following code? #include int main() { const char* p ="Hello"; std::string q = p + 'H'; std::cout << q << std::endl; } And it rightly thrown error for the the following…
Soumya dutta
  • 79
  • 1
  • 7
2
votes
0 answers

Template Function C++ Sqlite3 Wrapper

In the process of creating a C++ wrapper for anSqlite3 database. Below are two functions that (working together) fill an std::vector with the results from a query; both of which were successful before I tried to template them. I am getting a…
Austin
  • 195
  • 1
  • 4
  • 13
2
votes
1 answer

Write File byte[] array received from C# to a file in C dll

I just created a simple PDF Document containing a word "Test" in it and created a byte stream out of it in C# Console Application: buff = File.ReadAllBytes(); The size of the file is around 9,651 bytes. I also created a Win32 C dll…
learn_develop
  • 1,735
  • 4
  • 15
  • 33
2
votes
1 answer

How to append null terminator to end of the indexed char pointer

I ran the following code and it crashes with the while loop running forever. When I debugged this code, I found the problem at *(pointer+cnt)='\0'; the null character is never there. I don't know how to append the null terminator here so that the…
Rommel
  • 23
  • 3
2
votes
1 answer

How to pass a char* to the GetModuleHandle function?

I'm just trying to get the module information based on a string that can very well be something like "somefile.exe". MODULEINFO GetModuleInfo(char *szModule) { MODULEINFO modinfo = {0}; HMODULE hModule = GetModuleHandle(szModule); …
Gera
  • 65
  • 5
1 2
3
11 12