Discussion:
way of write *(int*)
(too old to reply)
a***@gmail.com
2017-05-05 10:56:31 UTC
Permalink
Raw Message
There is one alternative way of write *(int*)variablepointertochar
?
David Brown
2017-05-05 11:19:18 UTC
Permalink
Raw Message
Post by a***@gmail.com
There is one alternative way of write *(int*)variablepointertochar
?
How about:

char * variablePointerToChar;

or

char* variable_pointer_to_char;

or
int * called_pointer_to_char_but_really_pointer_to_int;


A bit more information about what you are trying to do, and the context
for it, would make it easier to give helpful answers.
a***@gmail.com
2017-05-05 11:53:26 UTC
Permalink
Raw Message
I would to like to write
*(int*)"string"
In few characters...
I find that
*(*)"string"
or
**&"string"
It is not ok because does not compile
or return one deference of pointer to char (so one char not one int)
David Brown
2017-05-05 13:02:53 UTC
Permalink
Raw Message
Post by a***@gmail.com
I would to like to write
*(int*)"string"
In few characters...
I find that
*(*)"string"
or
**&"string"
It is not ok because does not compile
or return one deference of pointer to char (so one char not one int)
I'm sorry, I have no idea what you are trying to say here.

Can you write a short program to demonstrate what you want to write
(even if it does not compile), along with the output that you expect to see?
a***@gmail.com
2017-05-05 13:31:21 UTC
Permalink
Raw Message
Thank you to all...
I will prove 'string'
Ben Bacarisse
2017-05-05 14:58:36 UTC
Permalink
Raw Message
Post by a***@gmail.com
Thank you to all...
I will prove 'string'
Try a union as in my reply to bartc.
--
Ben.
a***@gmail.com
2017-05-05 15:30:32 UTC
Permalink
Raw Message
It seems use of

*(int*)"4321"

can be reduced in Len by

'1234'

It is enough gcc or bcc compiler
32 bit int, 8 bit char
For me it is enough for reduce
size in codegolf answer...
Because base 256 reduce char size
The trick seems to me good for reduce size even for initialize ints or unsigned...

Thank you to Bartc and all
Ben Bacarisse
2017-05-05 16:36:00 UTC
Permalink
Raw Message
Post by a***@gmail.com
It seems use of
*(int*)"4321"
can be reduced in Len by
'1234'
They mean different things (if that makes sense since both a formally
undefined).

<snip>
--
Ben.
James Kuyper
2017-05-05 17:05:17 UTC
Permalink
Raw Message
Post by a***@gmail.com
It seems use of
*(int*)"4321"
can be reduced in Len by
'1234'
They mean different things ...
They are both expressions of type 'int'. It's entirely up to the
implementation whether they have the same value.
... (if that makes sense since both a formally
undefined).
The first expression has undefined undefined behavior for at least one
reason, and possibly as many as three, depending upon the implementation:

"A pointer to an object type may be converted to a pointer to a
different object type. If the resulting pointer is not correctly aligned
68) for the referenced type, the behavior is
undefined." (6.3.2.3p7). There's no guarantee that "4321" is correctly
aligned to store an int.

"An object shall have its stored value accessed only by an lvalue
expression that has one of the following types:" (6.5p7). The following
list of types does not include anything that covers this case.

"Certain object representations need not represent a value of the object
type. If the stored value of an object has such a representation and is
read by an lvalue expression that does not have character type, the
behavior is undefined. If such a representation is produced by a side
effect that modifies all or any part of the object by an lvalue
expression that does not have character type, the behavior is
undefined." (6.2.6.1p5). Whether "4321" contains the representation of a
valid int depends upon the implementation, the standard provides no
guarantees.

In contrast, the second expression is covered by:

"The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined." (6.4.4.4p10)

It seems to me reasonably plausible that the implementation-define value
of '1234' might be the same as that of *(int*)"4321".

bartc
2017-05-05 13:05:29 UTC
Permalink
Raw Message
Post by a***@gmail.com
I would to like to write
*(int*)"string"
In few characters...
I find that
*(*)"string"
or
**&"string"
It is not ok because does not compile
or return one deference of pointer to char (so one char not one int)
You might try:

'string'

but:

* It depends on the compiler; some will return only a single char code

* gcc will allow a char constant of multiple characters, but seems to be
limited to four characters, which will be the last ones ('ring' in the
example)

* Even with 2-4 characters, the layout in the int as stored in memory,
might not be the same as the equivalent string, if that is important.

There are any number of ways around this, such as the cast, but your
main concern seems to be brevity. In that case perhaps a 1-letter macro:

#define I(str) (*(int*)(str))

printf("%llX\n",I("ABCDEFGH"));

However, I don't think this counts as a compile-time constant, if that
is needed.


(FWIW, in my parallel language, this is well-behaved, so that [not C code]:

word64 a='string'

printf("%.8s\n", &a) # output: string
printf("%c\n", a iand 255) # ie. a & 255; output: s

and strings longer than 8 characters are truncated at the right end.
(They hadn't been, but they are now!) Such constants are also
compile-time ones.

This would be a nice little enhancement for C, but is probably far too
simple and useful to be considered.)
--
bartc
Ben Bacarisse
2017-05-05 14:57:55 UTC
Permalink
Raw Message
bartc <***@freeuk.com> writes:
<snip>
Post by bartc
word64 a='string'
printf("%.8s\n", &a) # output: string
printf("%c\n", a iand 255) # ie. a & 255; output: s
and strings longer than 8 characters are truncated at the right
end. (They hadn't been, but they are now!) Such constants are also
compile-time ones.
This would be a nice little enhancement for C, but is probably far too
simple and useful to be considered.)
typedef union { char s[sizeof (long long)]; long long i; } int_string;
...
long long a = (int_string){"string"}.i;
printf("%s\n", (char *)&a);

If you do a lot of stuff with these things, you might be better off
keeping them as a union:

int_string a = {"string"};
printf("%s\n", a.s);
--
Ben.
jak
2017-05-05 13:22:09 UTC
Permalink
Raw Message
Post by a***@gmail.com
There is one alternative way of write *(int*)variablepointertochar
?
char *c_ptr = "foo";
union
{
char *uc_ptr;
int *ui_ptr;
} uPtrs;
uPtrs.uc_ptr = c_ptr;
int *i_ptr = uPtrs.ui_ptr;
Barry Schwarz
2017-05-05 14:22:07 UTC
Permalink
Raw Message
Post by a***@gmail.com
There is one alternative way of write *(int*)variablepointertochar
?
Are you aware that the value of a char* may not be a valid value think
of alignment) for an int*? If that is the case, then you execution
invokes undefined behavior.

If your pointer points to an array that is smaller than sizeof(int),
the code invokes undefined behavior regardless of whether the address
in question is properly aligned or not.

The value extracted from such code is likely to be different on a
big-endian system than a little-endian one.

What is the real purpose of such code? Telling us that may allow
someone to give you a better solution.
--
Remove del for email
a***@gmail.com
2017-05-05 14:47:27 UTC
Permalink
Raw Message
It is only a codegolf question; some way for to gain reduction of code length even if it is one character...
j***@verizon.net
2017-05-05 15:24:07 UTC
Permalink
Raw Message
Post by a***@gmail.com
It is only a codegolf question; some way for to gain reduction of code length
even if it is one character...
You code is inherently non-portable, given that there's no guarantee that the contents of the screen can be interpreted as a valid 'int'. Can your code be even more unportable? It's possible that, on the particular compiler you're using, the integer constant 'string' has the same value as *(int*)"string".
Loading...