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

C, pointers and void functions

Have looked for a similar answer but nothing I try works. Have an issue, I want to change the value of word by calling the void function init() but when I print the word it does not work. Have spent way to many hours on this so any help would be…
-1
votes
1 answer

Why do I get an error when passing a string as a filename, but not a char*?

I understand that a char pointer is used to create strings in C (with a null terminator). But I don't understand why I am getting an error in C++ for passing a string as a file name, yet it works for a char*. The h prototype and cpp function…
user3956566
-1
votes
3 answers

char* and boolean TRUE FALSE in C

I have a piece of legacy code that has got char* function arguments which are used for if-then-else logical flow. For example: void myFunc(char *f_reset) { ..... ..... if(*f_reset) {// do this;} else {// do that;} } suppose I…
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
-1
votes
5 answers

Why does my program throw a segmentation fault while using heap-allocated memory?

After writing a program to reverse a string, I am having trouble understanding why I got a seg fault while trying to reverse the string. I have listed my program below. #include #include #include void reverse(char…
Srini
  • 21
  • 2
  • 5
-2
votes
1 answer

c++ Replace character of char pointer with char pointer

I'm having problemes with a function which should replace every character c in the given char array src with the char array text. first I calculate the length of the return value. Then I insert the correct chars. At least thats what i try. The…
-2
votes
1 answer

when using fread() on a partial line, how do you move to the next line?

Im using fread() and fseek() togather to gather parts of a string. I'm not using fread() on the whole line though. I'd take the whole line but to my knowledge you cannot use fseek() on a character array correct? `int parse(const char *f, const…
-2
votes
1 answer

How to read a sentence of char pointer with scanf()?

I have a char pointer: char *sentences; and I read it like this: #include int main() { char *sentences; sentences="We test coders. Give us a try?"; printf("%s", sentences); return 0; } but I want to read with scanf()…
Sillyon
  • 45
  • 1
  • 2
  • 11
-2
votes
4 answers

Looping char* gives me Access violation writing location 0x00CFB310

i try to print simple c* string like this : char *cc = "HEllo"; for (char* inputPtr = cc; inputPtr[0]; inputPtr++) { char c = inputPtr[0]++; printf("%s",c); } but im getting…
user63898
  • 29,839
  • 85
  • 272
  • 514
-2
votes
2 answers

((char) 257) prints 1 in C?

I am vastly confused. I'm having a headache. I'm new to C. #include int main() { int i = 257; int *iPtr = &i; printf("val 1: %d \t val 2: %d \n\n",*( (char*)iPtr ), *( (char*)iPtr+1) ); int iptr_alias = iPtr; int and_val = iptr_alias…
Tony K
  • 61
  • 3
  • 10
-3
votes
1 answer

Add a character to a character pointer which is initialized with a string in c

I declared a character pointer and allocated memory dynamically and initialized it with a string now i want to add character to the pointer. I tried the following code but it is giving segmentation fault(core dumped) error. #include int…
Gara Ranjeet
  • 13
  • 1
  • 3
-3
votes
1 answer

How to print right/center justified text from an index in C/C++?

I'm pretty bad at using char*'s in C/C++ and am asking for such an effect: Say I have a text editor or viewer and I want to show line numbers on the side. My implementation would be the following (please point out any faults in this if…
Apoqlite
  • 239
  • 2
  • 21
-3
votes
1 answer

I am at a loss for why this is code is giving me a read acess violation. dereferencing pointer and subtracting another char should work in Theory

I dunno why this doesn't work. the code has a problem with the *c in charToInt function but should be a legal statement in c. at least so I thought. I am excited to learn something new here. int charToint(char *c) { return *c - '0'; } int…
teaNcode
  • 19
  • 1
  • 7
-3
votes
1 answer

Get ASCII value from yytext

This maybe a silly question but I'm stuck on this. I'm doing an assignment on Compiler. I have to match a character literal. It's defined as any single character enclosed with ' ', with the exception of '\t','\n','\a' etc. In my lex file, I have…
-3
votes
1 answer

Character pointer and strings

Experts there is a question given in the book "Let us C" where the author has asked to write the output of the given program. The program is- #include int main() { char s[]="Churchgate: no church no gate"; char t[40]; char…
SGG
  • 31
  • 1
  • 2
  • 8
-3
votes
4 answers

Concate two char pointers

I am just learning C and got to the point of pointers. I wrote a method which should concate two char pointers and I got a strange behavior I just cant explain to myself nor did I find an answer on it, so maybe you can help! So I don't understand…
F. Zi
  • 41
  • 8
1 2 3
11
12