linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* literal constant..
@ 2004-02-03  6:46 Vadiraj C S
       [not found] ` <006601c3ea24$c694d230$ed64a8c0@descartes>
  2004-02-03 15:07 ` Glynn Clements
  0 siblings, 2 replies; 6+ messages in thread
From: Vadiraj C S @ 2004-02-03  6:46 UTC (permalink / raw)
  To: linux-c-programming


Hello Everyone!!

  I just joined the group. I have a doubt here, might sound simple but need to be cleared well,

what is the diff between the following statements..

  char *c="c" ;

  char c[1] = "c" ;

  I would be happy if I could get in depth explaination of it. 
  
 Thanks in advance
-- 
Regards
Vadiraj C S
	


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: literal constant..
       [not found] ` <006601c3ea24$c694d230$ed64a8c0@descartes>
@ 2004-02-03  7:34   ` Vadiraj C S
  0 siblings, 0 replies; 6+ messages in thread
From: Vadiraj C S @ 2004-02-03  7:34 UTC (permalink / raw)
  To: John T. Williams; +Cc: linux-c-programming

On Tue, 3 Feb 2004 02:10:16 -0500
"John T. Williams" <jowillia@vt.edu> wrote:

> char *a = "a";
> means create a pointer and point it at a point in memory that contains the
> string "a"
> 
> char b[1] = "b";
> means create a constant pointer and non-dynamic memory of size 1 char ( 1
> byte ), point the pointer to the memory and assign it the value of "b"
> 
> the major difference is that in the case of 'b', 'b' is a constant pointer
> to a specific place in memory on the stack.  while 'a' can point to any
> legal memory address.
> 


  Ok this is the asm code for the array decl case  c[1]="1".....
 
.file   "temp.c"
        .section        .rodata
.LC0:
        .string "1"
        .text
        .align 2
.globl main
        .type   main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movb    .LC0, %al
        movb    %al, -1(%ebp)
        movb    $50, -1(%ebp)
        leave
        ret
.Lfe1:
        .size   main,.Lfe1-main
        .ident  "GCC: (GNU) 3.2"

----------------------------
 This is for the pointer dicleration *c = "1" 
I do 


.file   "temp.c"
        .section        .rodata
.LC0:
        .string "1"
        .text
        .align 2
.globl main
        .type   main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movl    $.LC0, -4(%ebp)
        movl    -4(%ebp), %eax
        movb    $50, (%eax)
        leave
        ret
.Lfe1:
        .size   main,.Lfe1-main
        .ident  "GCC: (GNU) 3.2"


  I do c[0]="2" in both the case, the pointer case it fails cos the values should be constant. 

the diff is only two line

<       movb    .LC0, %al
<       movb    %al, -1(%ebp)
<       movb    $50, -1(%ebp)
---
>       movl    $.LC0, -4(%ebp)
>       movl    -4(%ebp), %eax
>       movb    $50, (%eax)

  I clould not get much about it, Please help me in this...


Thanks


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: literal constant..
@ 2004-02-03 11:41 Sandro Dangui
  2004-02-03 14:34 ` Florian Attenberger
  0 siblings, 1 reply; 6+ messages in thread
From: Sandro Dangui @ 2004-02-03 11:41 UTC (permalink / raw)
  To: Vadiraj C S; +Cc: linux-c-programming


The main difference (while programming) between variable "a" and "b" is that
you cannot use "b" in string operations. I mean, "b" does not contain the
character '\0' at the end, and if you use functions like strcmp(), strcpy(),
etc., you will have problems...
If you look the contents of "a" in memory you will see "a\0" (2 bytes). And
"b" in memory will be "b" (just one byte).

Sandro.


-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of Vadiraj C S
Sent: Tuesday, February 03, 2004 5:34 AM
To: John T. Williams
Cc: linux-c-programming@vger.kernel.org
Subject: Re: literal constant..


On Tue, 3 Feb 2004 02:10:16 -0500
"John T. Williams" <jowillia@vt.edu> wrote:

> char *a = "a";
> means create a pointer and point it at a point in memory that contains 
> the string "a"
> 
> char b[1] = "b";
> means create a constant pointer and non-dynamic memory of size 1 char 
> ( 1 byte ), point the pointer to the memory and assign it the value of 
> "b"
> 
> the major difference is that in the case of 'b', 'b' is a constant 
> pointer to a specific place in memory on the stack.  while 'a' can 
> point to any legal memory address.
> 


  Ok this is the asm code for the array decl case  c[1]="1".....
 
.file   "temp.c"
        .section        .rodata
.LC0:
        .string "1"
        .text
        .align 2
.globl main
        .type   main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movb    .LC0, %al
        movb    %al, -1(%ebp)
        movb    $50, -1(%ebp)
        leave
        ret
.Lfe1:
        .size   main,.Lfe1-main
        .ident  "GCC: (GNU) 3.2"

----------------------------
 This is for the pointer dicleration *c = "1" 
I do 


.file   "temp.c"
        .section        .rodata
.LC0:
        .string "1"
        .text
        .align 2
.globl main
        .type   main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp
        movl    $.LC0, -4(%ebp)
        movl    -4(%ebp), %eax
        movb    $50, (%eax)
        leave
        ret
.Lfe1:
        .size   main,.Lfe1-main
        .ident  "GCC: (GNU) 3.2"


  I do c[0]="2" in both the case, the pointer case it fails cos the values
should be constant. 

the diff is only two line

<       movb    .LC0, %al
<       movb    %al, -1(%ebp)
<       movb    $50, -1(%ebp)
---
>       movl    $.LC0, -4(%ebp)
>       movl    -4(%ebp), %eax
>       movb    $50, (%eax)

  I clould not get much about it, Please help me in this...


Thanks

-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: literal constant..
  2004-02-03 11:41 Sandro Dangui
@ 2004-02-03 14:34 ` Florian Attenberger
  2004-02-03 15:04   ` John T. Williams
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Attenberger @ 2004-02-03 14:34 UTC (permalink / raw)
  To: Sandro Dangui, linux-c-programming

char c[1]="c" is an unlucky example, it seems to be like this:
char c* = "c"; // make c point to "c\0".
char c[1] = "c"; // make c point to an undifined array of char of length 
1, set last char to '\0', _after_ that overwrite the array with "c", 
since "c" is just as long as the whole array, the \0 gets overwritten.

To get the second example behave equal to the first, it needs to be:
char c[2] = "c"; // char[0] == 'c', char[1] == '\0'.

So much to code difference.

char c[] is however a constant pointer, so you cannot reassign it.
That doesn't have to result in any assembler code difference at all, its 
just the compiler that complains.

flo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: literal constant..
  2004-02-03 14:34 ` Florian Attenberger
@ 2004-02-03 15:04   ` John T. Williams
  0 siblings, 0 replies; 6+ messages in thread
From: John T. Williams @ 2004-02-03 15:04 UTC (permalink / raw)
  To: linux-c-programming

it is actually easier to see the difference if you divorce the assignment
operation from the variable declarations.


char* a;
creates a pointer that points to nothing in particular ( its value is
unknown)

char b[2];
creates a constant pointer that points to a specific place in memory that
has 2 bytes set aside for its use.

I can then make the call

a = (char*) malloc( 300 );  // and a would now point to an array of 300
character in length (on the heap if you care)

however the call
b = (char*) malloc( 300); // is illegal, and should not compile.





----- Original Message ----- 
From: "Florian Attenberger" <fattenberger@onlinehome.de>
To: "Sandro Dangui" <sdangui@nortelnetworks.com>;
<linux-c-programming@vger.kernel.org>
Sent: Tuesday, February 03, 2004 9:34 AM
Subject: Re: literal constant..


> char c[1]="c" is an unlucky example, it seems to be like this:
> char c* = "c"; // make c point to "c\0".
> char c[1] = "c"; // make c point to an undifined array of char of length
> 1, set last char to '\0', _after_ that overwrite the array with "c",
> since "c" is just as long as the whole array, the \0 gets overwritten.
>
> To get the second example behave equal to the first, it needs to be:
> char c[2] = "c"; // char[0] == 'c', char[1] == '\0'.
>
> So much to code difference.
>
> char c[] is however a constant pointer, so you cannot reassign it.
> That doesn't have to result in any assembler code difference at all, its
> just the compiler that complains.
>
> flo
> -
> To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: literal constant..
  2004-02-03  6:46 literal constant Vadiraj C S
       [not found] ` <006601c3ea24$c694d230$ed64a8c0@descartes>
@ 2004-02-03 15:07 ` Glynn Clements
  1 sibling, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-02-03 15:07 UTC (permalink / raw)
  To: Vadiraj C S; +Cc: linux-c-programming


Vadiraj C S wrote:

>   I just joined the group. I have a doubt here, might sound simple
>   but need to be cleared well,
> 
> what is the diff between the following statements..
> 
>   char *c="c" ;
> 
>   char c[1] = "c" ;

The first statement creates a pointer variable, c, which is
initialised to point at the string literal "c", which is itself stored
in the read-only data segment.

The second statement creates an array variable, c, which is intialised
with the single character 'c'. Note that the array should have been
declared to hold two bytes, to include the terminating NUL character.

More generally: if you need to ask this question, you need to spend
more time learning the basics of C before trying to program in it.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-02-03 15:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-03  6:46 literal constant Vadiraj C S
     [not found] ` <006601c3ea24$c694d230$ed64a8c0@descartes>
2004-02-03  7:34   ` Vadiraj C S
2004-02-03 15:07 ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2004-02-03 11:41 Sandro Dangui
2004-02-03 14:34 ` Florian Attenberger
2004-02-03 15:04   ` John T. Williams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).