ayo-nya
ayo-nya

Reputation: 25

Difference between int*a and char *a?

What is the difference between char *a and int *a as both work on Code Blocks for storing character pointer eg.

char *c, k = '$';
int *j;

j = &k;
printf("%c\n%p\n%c\n", k, j, *j);

c = &k;
printf("%c\n%p\n%c", k, c, *c);

Upvotes: 1

Views: 2814

Answers (5)

Stack Man
Stack Man

Reputation: 579

What is the difference between a char pointer and an int pointer ?

Without considering what your code, the answer is simple Char pointer points to a memory address which holds a char value and an int pointer points to one with int value. This is and remains the difference between them. However, when you force the compiler to do something what they haven't been specified to, you either get an error or an unspecified behavior.

So, what's up with your code ?

That has already been explained well in answers but the basic thing is that char is stored as an ascii value and thus in your case an int pointer could point to a char.

Upvotes: 0

Baldrickk
Baldrickk

Reputation: 4409

char *a

a is a pointer to something. That something is a char

int *b

b is a pointer to something. That something is an int

Both a and b are pointers, they only store memory addresses to other things, which is why it is possible (but definitely not reccommended; warning by default, error with -wError) to store the address of an int in a char *.
Dereferencing it is undefined behaviour and "anything could happen" which is why the warning/error is there in the first place.

It may work with your current machine and compiler. It isn't guaranteed to though, and literally anything could break it. Don't do it

Upvotes: 1

Amit Sharma
Amit Sharma

Reputation: 2067

In your case it will give the same value because sizeof(int)>size(char). If you really want to see the difference between char* and int*. Lets do this:

Assume: char is of 1 byte, int is 4 byte and addresses are also of 4 byte. To observe the difference between char* and int*.

int k=1024;
char* charptr= &k;
int* intptr=&k;

printf("%02x \n\n", *charptr); // this will simply print `00`
printf("%02x \n\n", *intptr); // this will simply print `400`
int i=0;
for(; i<4 ; i++)
  printf("%02x ", *charptr++);  // this will print `00 04 00 00`

NOTE: It is a little endian machine. First print displays the content of first byte only therefore we see 00 as output. Third print statements clears everything.

Hope this will help to understand the difference here.

Upvotes: 0

2501
2501

Reputation: 25752

Interpreting a character object as an integer

printf("%c\n%p\n%c\n", k, j, *j);

or storing the address of a char into an int pointer

j = &k;

is undefined behavour.

In your case you got the same result by chance. The code is incorrect and may as well print anything.

Upvotes: 3

Deduplicator
Deduplicator

Reputation: 45654

Activate diagnostics and don't ignore them (-Wall -Wextra -pedantic-errors).
The compiler should tell you that you are doing something disallowed.

See here on coliru: http://coliru.stacked-crooked.com/a/31acb5b670254167

main.cpp:7:7: error: incompatible pointer types assigning to 'int *' from 'char *' [-Werror,-Wincompatible-pointer-types]
    j = &k;
      ^ ~~

Answering your question, a char is an integer-type of lower rank than int (meaning potentially (and in practice nearly guaranteed) smaller size and value-range), and thus pointers to either are different types too.
Using a pointer of wrong type to access an object (with few exceptions) is UB.

Upvotes: 5

Related Questions