ptr

V

vim

Hi everyone
I have a doubt in this program
#include<stdio.h>
main()
{
char str1[]="hello";
char *p=hello;
p="bye";
}
According to this program p is pointer to the string hello.When we made
to point to bye.Only 3 letters should erase.But it is giving bye .
Why?
 
H

HandledException

Hi!

Consider this small modificationt o your program:

#include<stdio.h>
main()
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);
p="bye";
printf("%u = %s",p,p);
}

Output to be observed:

4202496 = hello
4202510 = bye

which means that the two strings "hello" and "bye" are present at 2
different memory locations and by changing the address to which pointer
'p' points to you, the observed output is justified.

Please correct me if my interpretation is wrong.
 
V

Vladimir Oka

vim said:
Hi everyone
I have a doubt in this program

I have a doubt as well, since it doesn't compile at all:

gcc.exe -c main.c -pedantic -W -Wall -Wextra -ansi

In file included from main.c:1:
main.c:3: warning: return type defaults to `int'
main.c: In function `main':
main.c:5: error: `hello' undeclared (first use in this function)
main.c:5: error: (Each undeclared identifier is reported only once
main.c:5: error: for each function it appears in.)
main.c:4: warning: unused variable `str1'
make.exe: *** [main.o] Error 1
Execution terminated
#include<stdio.h>
main()
{
char str1[]="hello";
char *p=hello;
p="bye";
}
According to this program p is pointer to the string hello.When we made
to point to bye.Only 3 letters should erase.But it is giving bye .
Why?

[Provided you fix it, and add a `printf()` so it does output
something.]
Because what you assign to `p` is not a string "bye" but a pointer to a
constant string literal "bye". In case of `str1`, what you've written
is just shorthand for array initialisation:

char str1[] = {'h','e','l','l','o','\0'};

In the second line you probably meant:

char *p = "hello";

which again assigns to `p` a pointer to a constant string literal
"hello".

Constant string literals are held somewhere in memory (you don't know,
and shoudln't care), and you're not allowed to modify them, i.e.:

*p = 'Z';

is illegal.
 
V

Vladimir Oka

HandledException said:
Hi!

Consider this small modificationt o your program:

#include<stdio.h>
main()

int main(void)
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);

The only correct way to output pointers is using "%p" specifier.
p="bye";
printf("%u = %s",p,p);

If you don't terminate ouptut with a '\n', you may not see it at all
(you can use `fflush()` as well).

Also, not required in C99, but nice still:

return 0;

(NB, C99 wouldn't allow implicit type for `main()`, so your code is not
C99, and the `return` is, in fact, required.)
}

Output to be observed:

4202496 = hello
4202510 = bye

which means that the two strings "hello" and "bye" are present at 2
different memory locations and by changing the address to which pointer
'p' points to you, the observed output is justified.

Please correct me if my interpretation is wrong.

It's not interpretation, it's experimentation. You explained *what*
happens, not *how* or *why*. You also made errors in your "corected"
code which I pointed out above.

Quote context. Read <http://cfaj.freeshell.org/google/>.
 
V

vim

Sorry I have forgotten to put double quoutes before hello
The correct line code
char *p="hello";
 
C

CoL

The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

Regards,
COL
 
P

pete

CoL said:
The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

The illegality of

"bye"[0] = 'C';

stems from the standard explicitly saying that attempting
to modify a string literal, is undefined.
String literals are not const qualified.
 
G

Guest

pete said:
CoL said:
The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

The illegality of

"bye"[0] = 'C';

stems from the standard explicitly saying that attempting
to modify a string literal, is undefined.
String literals are not const qualified.

Indeed. So this

int main(void) {
if(0) "bye"[0] = 'C';
}

is valid, right? The attempt to modify a string literal is never made.
 
P

pete

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
CoL said:
The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

The illegality of

"bye"[0] = 'C';

stems from the standard explicitly saying that attempting
to modify a string literal, is undefined.
String literals are not const qualified.

Indeed. So this

int main(void) {
if(0) "bye"[0] = 'C';
}

is valid, right? The attempt to modify a string literal is never made.

That, my friend, is a strictly conforming program, in C99.
If you end main with a
return 0;
statement,
then it is just simply a strictly conforming program,
and any implementaion which will not translate it,
is not a conforming C implementation.
 
C

Charles Richmond

vim said:
Hi everyone
I have a doubt in this program
#include<stdio.h>
main()
{
char str1[]="hello";
char *p=hello;
p="bye";
}
According to this program p is pointer to the string hello.When we made
to point to bye.Only 3 letters should erase.But it is giving bye .
Why?
This belies a deeper misunderstanding of what is going on here.
"hello" and "bye" are disjoint strings. You are merely replacing
the pointer to the string "hello" with the pointer to the string
"bye". You are *not* altering the text in any way.
 
G

Guest

pete said:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
CoL wrote:

The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

The illegality of

"bye"[0] = 'C';

stems from the standard explicitly saying that attempting
to modify a string literal, is undefined.
String literals are not const qualified.

Indeed. So this

int main(void) {
if(0) "bye"[0] = 'C';
}

is valid, right? The attempt to modify a string literal is never made.

That, my friend, is a strictly conforming program, in C99.
If you end main with a
return 0;
statement,
then it is just simply a strictly conforming program,
and any implementaion which will not translate it,
is not a conforming C implementation.

Thank you. My compiler (GCC 4.1) rejects it, and as far as I can tell
no options whatsoever can convince it to accept it. I guess I should
open a bug report...
 
P

pete

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
pete wrote:
CoL wrote:

The Output will definitely be
only bye.
when the expression was
char *p="hello";
p is pointing to a constant staic string literal somewhere in memory.
The momemt you write
char *p="bye";
"bye" is an another constant staic string literal "somewhere(could be
anywhere)" in memory and now p starts pointing to it.
And since its constant staic in nature so any exression like
*p='C'; would be illegal by ANSI C standards.

The illegality of

"bye"[0] = 'C';

stems from the standard explicitly saying that attempting
to modify a string literal, is undefined.
String literals are not const qualified.

Indeed. So this

int main(void) {
if(0) "bye"[0] = 'C';
}

is valid, right? The attempt to modify a string literal is never made.

That, my friend, is a strictly conforming program, in C99.
If you end main with a
return 0;
statement,
then it is just simply a strictly conforming program,
and any implementaion which will not translate it,
is not a conforming C implementation.

Thank you. My compiler (GCC 4.1) rejects it, and as far as I can tell
no options whatsoever can convince it to accept it. I guess I should
open a bug report...

Yes. You should.

Here's another one you can try:

int main(void) { return 0 && (1 / 0); }

.... with references:

http://groups.google.com/group/comp.lang.c/msg/411fdf87cc03cfdf

And also if you have
char array[some number unequal to sizeof &array ];
there exist some nonconforming compilers
which will report (sizeof array) as being equal to (sizeof &array)
 
C

CBFalconer

pete said:
.... snip ...

Here's another one you can try:

int main(void) { return 0 && (1 / 0); }

To get gcc 3.2.1 to complain I have to use:

int main(void) { return (1 / 0) && 0; }
 
P

pete

CBFalconer said:
To get gcc 3.2.1 to complain I have to use:

int main(void) { return (1 / 0) && 0; }

As well it should.
I didn't mean that that program
was a problem specifically for gcc.
I only know it to be a problem
for certain Microsoft implementations.

The sizeof array, sizeof &array, equality problem,
I also know from Microsoft.

If =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
has more than one kind of compiler,
he can try those programs out.

Does your gcc have a problem with

int main(void)
{
if(0) "bye"[0] = 'C';
return 0;
}

like =?utf-8?B?SGFyYWxkIHZhbiBExLNr?='s GCC 4.1 does?
 
G

Guest

Same with GCC 4.1. That is, there is not even a warning for the first
case, and the warning is not fatal in the second case.
As well it should.
I didn't mean that that program
was a problem specifically for gcc.
I only know it to be a problem
for certain Microsoft implementations.

The sizeof array, sizeof &array, equality problem,
I also know from Microsoft.

Are you sure you don't mean sizeof(&array[0]) ? I suppose it doesn't
matter when the system where it's mishandled gives all pointers the
same size though. Anyway, I know of no version of GCC that doesn't
properly handle this.
If =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

I can understand if your newsreader doesn't decode this, and I don't
complain when you use this in attribution lines, but please know that
this is neither what I entered as my name nor what others see. (Not
necessarily all others, of course.)
has more than one kind of compiler,
he can try those programs out.

I have an installation of GCC 4.0.3, 4.1(20060517) and 4.2(20060513),
as well as TenDRA (4.1.2). All three GCC versions reject assignments to
"bye"[0]", but tcc accepts it. None have a problem with your other
examples.
Does your gcc have a problem with

int main(void)
{
if(0) "bye"[0] = 'C';
return 0;
}

like =?utf-8?B?SGFyYWxkIHZhbiBExLNr?='s GCC 4.1 does?

Older versions of GCC officially supported modifications of string
literals, given the right compiler options, so GCC 3.4 and below
shouldn't have problems with it. I'm interested too if they do, though.
 
P

pete

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
pete wrote:
The sizeof array, sizeof &array, equality problem,
I also know from Microsoft.

Are you sure you don't mean sizeof(&array[0]) ?

Yes.
This is the output from my microsoft implementation:

sizeof(char (*)[]) is 4
sizeof(&array) is 5
sizeof( array) is 5



/* BEGIN aname.c */

#include <stdio.h>

int main(void)
{
char array[1 + sizeof(char (*)[])] = {1};

printf(
"sizeof(char (*)[]) is %lu\n"
"sizeof(&array) is %lu\n"
"sizeof( array) is %lu\n",
(long unsigned)sizeof(char (*)[]),
(long unsigned)sizeof(&array),
(long unsigned)sizeof( array)
);
return 0;
}

/* END aname.c */


Quoting Jun Woong:
Why does MSVC have so many non-conforming features? :(
http://groups.google.com/group/comp.lang.c/msg/411fdf87cc03cfdf
 
G

Guest

pete said:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
pete wrote:
The sizeof array, sizeof &array, equality problem,
I also know from Microsoft.

Are you sure you don't mean sizeof(&array[0]) ?

Yes.
This is the output from my microsoft implementation:

sizeof(char (*)[]) is 4
sizeof(&array) is 5
sizeof( array) is 5 [...]
char array[1 + sizeof(char (*)[])] = {1};

How odd. Thanks for the info.
 
B

Barry Schwarz

Hi!

Consider this small modificationt o your program:

#include<stdio.h>
main()

int main(void)
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);

The only correct way to output pointers is using "%p" specifier.

But %p also requires a void*. Since p is a char* in this case, it
will have the same representation as a void* but casting to void* is
still a good habit to get into in this case.



Remove del for email
 
V

Vladimir Oka

Barry Schwarz opined:
Hi!

Consider this small modificationt o your program:

#include<stdio.h>
main()

int main(void)
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);

The only correct way to output pointers is using "%p" specifier.

But %p also requires a void*. Since p is a char* in this case, it
will have the same representation as a void* but casting to void* is
still a good habit to get into in this case.

Very true. I should have provided an example line as well.

printf("%p = %s\n", (void *)p, p);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top