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
32
votes
2 answers

Indexing an `unsigned long` variable and printing the result

Yesterday, someone showed me this code: #include int main(void) { unsigned long foo = 506097522914230528; for (int i = 0; i < sizeof(unsigned long); ++i) printf("%u ", *(((unsigned char *) &foo) + i)); putchar('\n'); …
18
votes
2 answers

void* vs. char* pointer arithmetic

I'm looking through my textbook and I'm a little confused about some of the code in there. In one part, they are performing pointer arithmetic in the following way: void* bp; ... bp = (void*)((char*)(bp)+16); ... But later on, they do the…
de1337ed
  • 3,113
  • 12
  • 37
  • 55
14
votes
12 answers

initializing char pointers

I have a char pointer which would be used to store a string. It is used later in the program. I have declared and initialized like this: char * p = NULL; I am just wondering if this is good practice. I'm using gcc 4.3.3.
ant2009
  • 27,094
  • 154
  • 411
  • 609
10
votes
3 answers

Overload operator[] for Char assignment - C++

I am fairly new to C++, although I do have some experience programming. I have built a Text class that uses a dynamic char* as it's main member. The class definition is below. #include #include using namespace std; class…
Joe
  • 797
  • 1
  • 10
  • 23
8
votes
5 answers

How to know when a char* library function arg needs an array it can modify, not a char pointer

I am new to C programming. and I know char * and char[] array are different. Yet, you can deduct the char[] to char * when it comes to a function param. So function declarations could be the same. But how do I know if the function is specifically…
asisas07
  • 91
  • 5
8
votes
1 answer

JNI. How to get jstring from jobject and convert it to char*

This is what I have so far: I pass an object which has 2 fields: String and Integer, as a parameter and I want to send information to process it in C part, which is not important at this point... I get complains at jstring declaration JNIEXPORT…
Aleksandr
  • 91
  • 1
  • 2
  • 7
7
votes
1 answer

How to pass entire collection(char**) of command line arguments as read-only in c++?

Suppose I have a program like this: #include #include #include // Takes values and outputs a string vector. std::vector foo(const int argc, char* args[]) { std::vector strings; for (int…
7
votes
2 answers

How can I safely ensure a char* type will be correctly implemented (on any platform) according to OpenGL spec?

In trying to get my head around graphics programming using c++ and OpenGL3+ I have come across a slightly specialized understanding problem with the char type, the pointers to it and potential implicit or explicit conversion to other char pointer…
eSemmel
  • 73
  • 5
6
votes
3 answers

Can I Initialize a char[] with a Ternary?

I asked a question about it and didn't get a really clear answer, but after reading this article I started preferring const char[] to const char*. I've come upon a difficulty when initializing with a ternary. Given const bool bar, I've tried: const…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
2 answers

Is accessing members through offsetof well defined?

When doing pointer arithmetic with offsetof, is it well defined behavior to take the address of a struct, add the offset of a member to it, and then dereference that address to get to the underlying member? Consider the following example: #include…
Ben Steffan
  • 1,095
  • 12
  • 16
6
votes
2 answers

Pointer arithmetics on non-array types

Let's consider following piece of code: struct Blob { double x, y, z; } blob; char* s = reinterpret_cast(&blob); s[2] = 'A'; Assuming that sizeof(double) is 8, does this code trigger undefined behaviour?
SergeyA
  • 61,605
  • 5
  • 78
  • 137
5
votes
3 answers

Incompatibility between char* and unsigned char*?

The following line of code produces a compiler warning with HP-UX's C++ compiler: strcpy(var, "string") Output: error #2167: argument of type "unsigned char *" is incompatible with parameter of type "char *" Please note: var is the unsigned…
Hassaan
  • 253
  • 4
  • 13
5
votes
1 answer

What does (char*) 0 mean?

This is a question in reference to this question: What does (char *)0 mean in c? There the answers slightly deviated away from explaining what exactly the answer was, but the final answer mentioned that it was a pointer to a character at address 0…
theprogrammer
  • 1,698
  • 7
  • 28
  • 48
5
votes
1 answer

stringWithUTF8String returning nil since iOS 8.2 update

I've been using stringWithUTF8String to convert my NSData to NSString as follows: if ([[NSString stringWithUTF8String:[responsedata bytes]] isEqualToString:@"SUCCESS"]){ dostuff... } It's been working fine; however, since the 8.2 iOS update,…
Stephan
  • 881
  • 9
  • 24
5
votes
3 answers

What does a pointer to a character point to in C?

I am trying to learn pointers in C, and have gone through the concepts. I came across this lab question, and tried to write a solution for it. /* p1.c Write a short C program that declares and initializes (to any value you like) a double, an int,…
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
1
2 3
11 12