1
#include <stdio.h>

int j;
int *ptr;

int main(void)
{
    j = 13232323;//adress I want to assign
    ptr = j;//assign the adress to the pointer
    printf("%d",ptr);
}

OUTPUT:13232323

Am I doing wrong as assigning adress directly to a pointer? Pointer is nothing but a variable contains value in the address form, so I constructed an address and then assign it to the pointer,it works as it was supposed to be, except that I can not construct an address containing characters ABCDEF,so,what's the big difference between int and pointer?

EDIT: This code means nothing but solely for testing purpose

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
user2556058
  • 311
  • 4
  • 9

7 Answers7

6

Actually what your trying is out of your eagerness., I agree, I too often do this way.

The Very first thing is if you store 13232323 in a pointer variable, the hex value of it is OXC9E8C3., so really at the time of your assigning the pointer variable (ptr) doesnot know whether really it is a valid address or invalid address. But when you dereference this address with *ptr, then comes the problem. It tries to seek the value in the address. Then there are 2 cases..

  1. Really if what you assigned is a valid address , then it will return the value. (Practically impossible case)
  2. Mostly your address will be a invalid one, hence program will crash (Segmentation fault).

So, even though your program compiles, runs, until and unless you store a valid address in ptr., ptr has no use.

Your Q: It works as it was surposed to be,except that I can not construct an address containing characters ABCDEF,so,what's the big difference between int and pointer? printf("%d",ptr);

I think you are asking, whatever the case, I cannot store ABCDEF, hence ptr works same as int type, so what is the difference between integer and pointer?

Here it is :

  1. You cannot deference an integer value, where as pointer can do it. Hence it is called as pointer :)
  2. You are seeing only numbers because you are printing the address with %d, trying printing with %x or %p.

Atlast, do you notice the compiler warning, warning: assignment makes pointer from integer without a cast , because ptr = j; in that ptr is of int* type, and j is of int type.

Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
  • Thanks for your answer,what would happen if I ignore that warning(don't cast to int *),so far everything works fine, – user2556058 Jul 14 '13 at 06:46
  • @user2556058 Then too Nothing will happen, the program will work fine. But `ptr` will become unusable. So, no use of `ptr`. But if you have tried to deference the ptr using (*ptr) at any moment, the program will crash. Until and unless you are not going to deference the (*ptr) there are no issues. But still your practice will be wrong. – Muthu Ganapathy Nathan Jul 14 '13 at 06:52
  • I'v made changes to my code like this : ptr = (int *)j; *ptr = 12; When I print ***ptr**,it crashed,no matter how many times I've changed the address to expect an used space,it always crashed,so,what could possiblely make it crash? – user2556058 Jul 14 '13 at 07:08
  • @user2556058 `Really what you assigned is a valid address , hence it will return the value. (Rare cases)`. I think you caught up with this statement. `int k=3; ptr = &k; *ptr=12;' Now print the `*ptr` . Actually, by trail and error you cannot guess a valid address like &k, bcz its really run-time dependent. Also, you could guess not in rare cases, where as in hypothetical(practically impratical case). – Muthu Ganapathy Nathan Jul 14 '13 at 08:19
  • 1
    @user2556058 Behavior of your code is behavior is Undefined at runtime read [**Invalid address causes Undefined behavior at runtime**](http://stackoverflow.com/questions/17627394/why-do-we-store-string-in-a-character-pointer-e-g-in-fopen/17627415#17627415) Example-2 – Grijesh Chauhan Jul 14 '13 at 09:12
4

you need to use %x, or you can send *ptr to the printf

printf("%d", *ptr);// if you are using this you need to know what are you pointing at, and that it will be an integer

or

printf("%x", ptr);

as the comments below says, it is always better to use %p instead of %x because %x invoke undefined behaver. also when using %p one should cast his pointer to (void *)

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
2

Your code is invalid. C language does not allow this assignment

ptr = j;

Integer values cannot be converted to pointer types without an explicit cast (constant zero being the only exception).

If the code was accepted by your compiler, it simply means that your compiler extends the language beyond its formal bounds. Many compilers do that in one way or another. However, if your compiler accepted this code without issuing a diagnostic message (a warning or an error), then your compiler is seriously broken.

In other words, the behavior of our program has very little (or nothing) to do with C language.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

In C you can do most of the type conversion, even the types are totally unrelated. It may cause warning though. make sure that your type conversion make sense.

lulyon
  • 6,707
  • 7
  • 32
  • 49
1

Syntactically, you are not doing anything wrong here. You can anyways, assign one pointer to another, and since they only contain addresses like the one you assigned here, this is what actually happens during the pointer assignment.
But you should never assign a random address and try to dereference it, because your program doesn't have access to a memory location unless alloted by the machine. You should request the memory from the machine using a function like malloc().
As for differences between pointers and integers, the biggest one is that you can't use a dereference operator (*) on an integer to access the value at the address it contains.
And you can construct addresses using ABCDEF by prefixing your address value with 0X or 0x to signify a hexadecimal notation.

user1925405
  • 332
  • 1
  • 5
  • 13
1
  • Am I doing wrong as assigning adress directly to a pointer?

Yes, the only integer types that are supposed to work directly with pointers as you do are intptr_t and uintptr_t, if they exist. If they don't exist you are not supposed to do this at all. Even then you would have to use an explicit cast to do the conversion.

  • Pointer is nothing but a variable contains value in the address form,so I constructed an adress and then assign it to the pointer,it works as it was surposed to be,except that I can not construct an address containing characters ABCDEF,so,what's the big difference between int and pointer?

No, on some architectures pointers can be more complex beasts than that. Memory can e.g be segmented, so there would be one part of the pointer value that encodes the segment and another that encodes the offset in that segment. Such architectures are rare nowadays, nevertheless this is something that is still permitted by the C standard.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

you need ptr to hold the address of j by doing the following

ptr = &j;

ptr hold, equal to, the address of j

and you want to print the content of whats ptr is pointing to by the following

printf("%d",*ptr);

and to get the address of ptr do the following

printf("%x",ptr);

x for the hex representation

aah134
  • 860
  • 12
  • 25
  • You are doing things the normal way,but I want to assign an address manually rather than using & to get the address,so how can I assign a certain address to the pointer,like ABCDE1 – user2556058 Jul 14 '13 at 06:26
  • 1
    you make it equal to any address you like, but you might access illegal address location, and that might give you some undefined behavior, or even a segmentation error, core dump. – aah134 Jul 14 '13 at 06:28
  • Thanks for the heads up,I know it now – user2556058 Jul 14 '13 at 06:39