Dereferencing an int pointer vs char pointer

char pointer vs int pointer c
difference between char pointer and int pointer in c
pointer vs integer
pointers in c
char pointer to int pointer c
char pointer array in c
char vs int
char pointer in c

Assume this code in C:

int a = 5;
int *b = &a;
*b = 6;

Notice that b had to be dereferenced using * to have a value reassigned.

However, the same doesn't go with a char pointer (a String):

char *name = "test";
name = "test2";

name did not have to be dereferenced. Why is that?

In this code snippet

int a = 5;
int *b = &a;
*b = 6;

the last assignment stores the integer constant to the object pointed to by the pointer b. That is the value of the object a is changed not the value (address) stored in the pointer b.

In this code snippet

char *name = "test";
name = "test2";

the value of the pointer itself is changed not the object pointed to by the pointer. So at first the pointer name pointed to the first character of the string literal "test" and then it is reassigned to point to the first character of the string literal "test2".

It is the similar to the following code

int a = 5;
int *b = &a;

int a2 = 6;
b = &a2;

If you want to change an object pointed to by a pointer of the type char * you could write

char s[] = "test";
char *name = s;
*name = 'T';

In this case the array s will have "Test".

Pay attention that you may not change a string literal. That is if instead of the array s you will write

char *s = "test";
char *name = s;
*name = 'T';

then the code will have undefined behavior.

Also bear in mind that in this declaration

char *s = "test";

the string literal having the type char[5] is implicitly converted to pointer to its first element.

What is the difference between character pointer and integer pointer , As pointer, there's basically no difference. The difference is when dereferencing the pointers and assigning values. Type char is a byte wide type. This means, it� As pointer, there's basically no difference. The difference is when dereferencing the pointers and assigning values. Type char is a byte wide type. This means, it can store 8 bits of information. So type char can store numbers from -127 to 126. Type int is a 4-byte wide type, so it can store 32 bits of information.

The type of "test2" is already char[6] (which in turn decays to char*). There's simply no need to use address-of on the rvalue, or dereference the lvalue to do the assignment for the types to work out.

Pointer Basics, Pointers are used to store the addresses of other variables or memory items. a pointer to a double char * c1; // a pointer to a character float * fptr; // a pointer to a pointer-to-int // Notation: // ptr refers to the pointer itself // *ptr the dereferenced� Next, we print charptr value that points to char A which was a character variable that we declared earlier and is pointed by the void pointer. Next, we have assigned an integer variable to the void pointer and then we carry out the same steps of dereferencing this void pointer by using an integer pointer. Arrays And Pointers

Why is that?

Because the two examples you show are not the same.

The were the same if you did:

/* your 1st example: */
int a = 5;
int *b = &a; /* b is a pointer to a what a is. */
*b = 6;

a equals 6 now.

/* your second example adjusted: */
char *a = "test";
char **b = &a; /* b is a pointer to a what a is. */
*b = "test2";

a points to "test2" now.

The 5-Minute Guide to C Pointers, int main(int argc, char **argv). {. // declare int ival and int pointer iptr. Assign address of ival to iptr. int ival = 1;. int *iptr = &ival;. // dereference iptr� Any Operation performed on the de-referenced pointer directly affects the value of variable it pointes to. [/box] Live Example : Dereferencing Pointer // Sample Code for Dereferencing of Pointer int n = 50, x ; int * ptr ; // Un-initialized Pointer ptr = & n; // Stores address of n in ptr x = * ptr; // Put Value at ptr in x Evaluation

Everything you need to know about pointers in C, If the type of a variable containing a pointer to int is int *,; and a single statement can It's also possible to write to a dereference expression (the C way of saying this: a struct foo { size_t size; char name[64]; int answer_to_ultimate_question; � Due to the ability of a pointer to directly refer to the value that it points to, a pointer has different properties when it points to a char than when it points to an int or a float. Once dereferenced, the type needs to be known. And for that, the declaration of a pointer needs to include the data type the pointer is going to point to.

Char pointer dereference? Char array vs Char*?, Why don' we have to dereference char pointer like we do with example int* ? Code example #1. Also, what is the difference between char* and� In other words - to dereference double pointer (another level of indirection). Now that still assumes, that the finall function is void(*)() type, but I would like to make cast available to other function "types" that can forexample accepts some arguments.

6.7 — Introduction to pointers, Pointers. With the address-of operator and dereference operators now added to (because char reads up to 1 byte and int is 4 bytes) and when dereferencing� an integer may be added to a pointer ( + or += ) an integer may be subtracted from a pointer ( – or -= ) Pointer arithmetic is meaningless unless performed on an array. Note : Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to.

Comments
  • When you de-reference a pointer, you reference the object that the pointer points to. So in the above assignment, you assign 6 to the variable a, not the pointer b.
  • Because the value of name isn't a char, but a char*.