Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String Literals

Reply
Thread Tools

String Literals

 
 
thedude
Guest
Posts: n/a
 
      07-22-2010
If I used this return statement in a c function

return (int)* "hello world";

From what I know the calling function will only retrieve the character
'h', right ?

Is it right to assume that nevertheless, read only memory was
allocated for the entire string "hello world" , And if so can that
memory(Or the rest of the characters) be accessed somehow ?

Thanks in advance.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-22-2010
thedude <> wrote:
> If I used this return statement in a c function


> return (int)* "hello world";


Why would you do that?

> From what I know the calling function will only retrieve the character
> 'h', right ?


The caller will receive the address of the string literal
cast to an int pointer. What that should be good for is
beyond me since the correct type of the address is const
char pointer. This conversion to int pointer invokes un-
defined behaviour. The standard allows to convert an ob-
ject pointer (e.g. an int *) to a void (or, for historical
reasons, char) pointer and then the back-conversion of this
result, but not from every object pointer type to each other
object pointer type (on some architectures different types
of pointers may have e.g. different sizes).

If you dereference the int pointer you got back from the func-
tion you may get a bus error since an int may have more strict
alignment requirements than a char, so the string may start at
an address that's not suitable aligned for accesses via an int
pointer (even if the cast from const char * to int * didn't
mess up the address).

> Is it right to assume that nevertheless, read only memory was
> allocated for the entire string "hello world"


Yes, with and without the "nevertheless"

> And if so can that
> memory(Or the rest of the characters) be accessed somehow ?


Yes and no. If your machine actually doesn't do anything
bad when converting from const char * to int * then you may
be able to convert the returned pointer back to const char *;
so you can get at the string. But why rely on that? If your
function is supposed to return a generic pointer you should
use a void pointer.
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Sebastian
Guest
Posts: n/a
 
      07-22-2010
On 22 Jul., 15:47, thedude wrote:
> If I used this return statement in a c function
>
> return (int)* "hello world";
>
> From what I know the calling function will only retrieve the
> character 'h', right ?


Please provide a code snippet that actually compiles or at least is
supposed to compile. I'm guessing you meant something like this:

char* foo() {
return "hello world";
}

In this case the function returns a pointer that points to the
character 'h', yes. But all the characters are placed consecutivly in
memory.

> Is it right to assume that nevertheless, read only memory was
> allocated for the entire string "hello world" ,


Yes. At least you are not allowed to change any of it. Prefer to use
the type "const char*" in such cases. This makes any mutation attemp a
compile-time error.

int main(void) {
const char *p = "hello";
p[0] = 'H'; // compile-time error
return 0;
}

whereas without "const" this program would probably compile on all
compilers but invoke undefined behaviour.

> And if so can that memory(Or the rest of the characters) be
> accessed somehow ?


Sure. A string literal is a character array with static storage. As
such, all the characters are stored linearly in memory.

#include <stdio.h>

int main(void) {
const char *p = "hello";
while (*p) printf("%c",*p++);
puts("\n");
return 0;
}

This program iterates through the whole string by incrementing the
pointer until the null terminator is reached. You see, the string
literal "hello" is an array of 6 characters: 'h', 'e', 'l', 'l', 'o',
'\0'.

Cheers
SG
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-22-2010
(Jens Thoms Toerring) writes:

> thedude <> wrote:
>> If I used this return statement in a c function

>
>> return (int)* "hello world";

>
> Why would you do that?
>
>> From what I know the calling function will only retrieve the character
>> 'h', right ?

>
> The caller will receive the address of the string literal
> cast to an int pointer. [snip]


You may want to look again. The cast is to (int),
not to (int*).
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-22-2010
Tim Rentsch <> wrote:
> (Jens Thoms Toerring) writes:


> > thedude <> wrote:
> >> If I used this return statement in a c function

> >
> >> return (int)* "hello world";

> >
> > Why would you do that?
> >
> >> From what I know the calling function will only retrieve the character
> >> 'h', right ?

> >
> > The caller will receive the address of the string literal
> > cast to an int pointer. [snip]


> You may want to look again. The cast is to (int),
> not to (int*).


Ooops, where are my glasses when I need them? Thanks...

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-22-2010
thedude <> writes:

> If I used this return statement in a c function
>
> return (int)* "hello world";
>
> From what I know the calling function will only retrieve the character
> 'h', right ?


Only the first character of the string literal will
be accessed, if that's what you mean.

> Is it right to assume that nevertheless, read only memory was
> allocated for the entire string "hello world" ,


It may be but it doesn't have to be. An implementation
may (legally) compile this the same as it would compile

return (int) 'h';

> And if so can that
> memory(Or the rest of the characters) be accessed somehow ?


Memory for the string literal need not exist in the
actual compiled program.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-22-2010
Sebastian <> writes:

> On 22 Jul., 15:47, thedude wrote:
>> If I used this return statement in a c function
>>
>> return (int)* "hello world";
>>
>> From what I know the calling function will only retrieve the
>> character 'h', right ?

>
> Please provide a code snippet that actually compiles or at least is
> supposed to compile. [snip]


I'm guessing you misread the example line. That's '(int)*',
not '(int*)'.
 
Reply With Quote
 
Sebastian
Guest
Posts: n/a
 
      07-22-2010
On 22 Jul., 16:14, Sebastian wrote:
> On 22 Jul., 15:47, thedude wrote:
>
> > If I used this return statement in a c function

>
> > * return (int)* "hello world";

>
> > From what I know the calling function will only retrieve the
> > character 'h', right ?

>
> Please provide a code snippet that actually compiles or at least is
> supposed to compile. I'm guessing you meant something like this:
>
> * char* foo() {
> * * return "hello world";
> * }


Oops.

return (int)* "hello world"

is actually legal for a function that returns int. I was assuming you
made a typo and meant (int*). But it also makes little sense. *"hello
world" is pretty much equivalent to 'h'. In that case the function
returns only an integer representing the 'h' character and you won't
be able to retrieve anything else because the address is lost.

You should have posted the whole function's body.

> Cheers
> SG

 
Reply With Quote
 
thedude
Guest
Posts: n/a
 
      07-22-2010
Thanks for your response Tim R.(and everyone else)

Yes I did mean dereference the string literal and cast it to an int.

Problem is, That function is in an "obnoxious" header file(API) I am
supposed to use(ie not in my control).
And I saw that only the first character was being returned, And why
they did that was beyond me too.

Ravi

On Jul 22, 10:27*am, Tim Rentsch <t...@alumni.caltech.edu> wrote:
> Sebastian <s.gesem...@gmail.com> writes:
> > On 22 Jul., 15:47, thedude wrote:
> >> If I used this return statement in a c function

>
> >> * return (int)* "hello world";

>
> >> From what I know the calling function will only retrieve the
> >> character 'h', right ?

>
> > Please provide a code snippet that actually compiles or at least is
> > supposed to compile. [snip]

>
> I'm guessing you misread the example line. *That's '(int)*',
> not '(int*)'.


 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-22-2010
On Jul 22, 5:42*pm, thedude <myclcontac...@gmail.com> wrote:
> Thanks for your response Tim R.(and everyone else)
>
> Yes I did mean dereference the string literal and cast it to an int.
>
> Problem is, That function is in an "obnoxious" header file(API) I am
> supposed to use(ie not in my control).
> And I saw that only the first character was being returned, And why
> they did that was beyond me too.
>

It seems odd.
However it's quite common to pass chars around as ints. For some
reason the character 'h' must have been required, and the reason it
was 'h' and not some other value was that 'h' is the initial character
of the string "hello world". That would make sense - if the crucial
string turns to "goodbye cruel world" at some later date the source of
the 'h' is documented for the maintainer.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Unicode String Literals didster@gmail.com Java 4 11-21-2005 11:43 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
character literals and string Pete Elmgreen Java 3 11-24-2004 04:42 PM
String literals in Java Harri Pesonen Java 59 06-02-2004 08:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57