tags:

views:

234

answers:

8
printf("%s", (char*)2189672);

2189672 is output of

char hi[4] = "hi!";
int *hii = (int*) hi;
printf("%d", *hii);

ah im an idiot! I would need a & infront of the number aswell.

Any way to get ptr to a constant? (2189672) without making another ptr variable?

+7  A: 

Because 2189672 is not a valid address in your processes address space.

anon
A: 

Why are you doing that?

The seg fault is likely do to the fact that you have no rights to the memory location 2189672.

ezpz
A: 

You can cast any number to a pointer, but that does not mean you have the rights to use them.

Use the proper allocation and deallocation functions to access memory.

Gamecat
+5  A: 

It looks as if you're trying to determine if the address of the character array really is there. But you're doing it wrong; you're dereferencing the pointer to integer hii, which will interpret the data it's pointing at (the string) as an integer, and then trying to use that integer as an address.

Note that 2189672 decimal is 0x00216968 in hex, and if we look at these as ASCII codes, we get:

  • 0x68 = 'h'
  • 0x69 = 'i' ('h' + 1, makes sense!)
  • 0x21 = '!'
  • 0x00 is the implicit terminator in the string.
unwind
+1  A: 

If you're wanting to print the value of a four-character constant that was passed in as an integer, you have to store the thing somewhere first so you can get its address to pass to printf().

What I usually wound up doing was something like:

void PrintFourChar(FILE* of, int fourChar)
{   union
    {   int   i[2];
        char  c[8];
    } x;

    x.i[0] = fourChar;
    x.c[4] = 0;
    fprintf(of, "%s", x.c);
}

Note that you're only going to get the correct output if the machine that runs this function has the same byte order as the machine that generated the int from the string in the first place.

(Note: Mac OS 9 and earlier made extensive use of these four-character constants. For comparison and tree-search purposes, you just treated them like integers (constant-time copy and compare, better than an arbitrary string), and for display purposes, you'd use a function like the above. They did this because it was easier for a developer to remember that "ALRT" was an alert box resource, "CODE" was a code resource, "STR " was a string, and so forth. They didn't allow control characters, though the OS generally didn't trap for them.)

EDIT: To be completely portable, I should have pulled in <stdint.h> and used int32_t in place of int.

Mike D.
hey, why array of 2 ints? and 8 chars when it seems you only use 5?
tm1rbrt
Because if the various alternatives in the union are not the same size, the compiler might pad your data in an unexpected way. Sure, it probably won't, but it doesn't save you any memory not to pad things to 64 bits yourself.
Mike D.
A: 

Let me get this straight: you're taking a character string, treating the contents of the string as an integer value, and then trying to use that integer value as a pointer to something else, assuming that something else is a 0-terminated string?

Why on earth did you think that would do anything meaningful? The odds that 2189672 corresponds to a) an accessible memory address that b) contains a 0-terminated string are negligible.

John Bode
+1  A: 

You are asking "Any way to get ptr to a constant?"

There's no way to get a pointer to a constant. A constant is not an lvalue, meaning that it has no address in storage. And since it has no address in storage, there's no way to create a pointer to it.

AndreyT
A string literal is a constant value, and you can certainly create a pointer to one.
anon
I assume that we are talking abut C. The term "constant" in C language has a very specific meaning. In C there are integer constants, floating constants, character constants and enum constants. That's it. The first three are referring to explcit literal values. The last one is self-explanatory. None of them are lvalues for obvious reasons. String literals in C are not "constants".
AndreyT
So you are saying that const int ci = 123; does not create a constant called ci?
anon
Firstly, yes, that's exactly what I'm saying. In C language `ci` is not a *constant*. It is a const-qualified object ("variable", if you wish), but not a *constant*. For example, look up the definition of an Integer Constant Expression (ICE) in C. It allows *integer constants*. Yet, in C (as opposed to C++) a mere `ci` is not an ICE. (I hope you know that). How come? Because `ci` is not allowed in ICE by the language specification. `ci` is *not* an integer constant in standard C parlance.
AndreyT
Secondly, string literal in C language is not const-qualified (as opposed to C++ again). Even in C99 string literal has type `char[N]` (not `const char[N]`), i.e. there's no `const` there. In C string literal in an array of non-modifiable elements, but not const-qualified and not even remotely a "constant" is the formal sense of the term.
AndreyT
One more time: aside from enums in C language the term *constant* (integer, character, floating-point) stands for what we call *literal* in C++ (integer, character, floating-point). I have to admit I'm more than a little surprised I have to explain it to *you*, Neil.
AndreyT
+1  A: 

No way to get a pointer to a literal in C.

In C++, you can get a reference to a constant.

In C#, there's a technique named 'boxing' for doing what you said.

Pavel Radzivilovsky