473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ptr

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?

May 17 '06 #1
19 1378
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.

May 17 '06 #2

vim wrote:
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.

May 17 '06 #3

HandledException wrote:
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/>.

May 17 '06 #4
vim
Sorry I have forgotten to put double quoutes before hello
The correct line code
char *p="hello";

May 17 '06 #5

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


What code? Quote context (even yourself). See
<http://cfaj.freeshell.org/google/>.

May 17 '06 #6
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

May 18 '06 #7
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.

--
pete
May 18 '06 #8
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.

May 18 '06 #9
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

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.

--
pete
May 18 '06 #10
vim wrote:

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.
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
May 18 '06 #11
pete wrote:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

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...

May 18 '06 #12
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

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

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....1fdf87cc03cfdf
int main(void) { return 0 && (1 / 0); }
Yes, this is a s. c. program. IIRC, the Standard requires an
implementation not to fail to translate and to execute a program
unless every possible execution of the program results in undefined
behavior; if every possible execution of a program results in
undefined behavior, then an implementation is allowed to fail to
translate it.


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)

--
pete
May 18 '06 #13
pete wrote:
.... 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; }

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
May 19 '06 #14
CBFalconer wrote:

pete wrote:

... 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; }


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?

--
pete
May 19 '06 #15
pete wrote:
CBFalconer wrote:

pete wrote:
... 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; }


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.

May 19 '06 #16
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

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....1fdf87cc03cfdf

--
pete
May 19 '06 #17
pete wrote:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

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.

May 19 '06 #18
On 17 May 2006 08:24:12 -0700, "Vladimir Oka" <no****@btopenworld.com>
wrote:

HandledException wrote:
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
May 21 '06 #19
Barry Schwarz opined:
On 17 May 2006 08:24:12 -0700, "Vladimir Oka"
<no****@btopenworld.com> wrote:

HandledException wrote:
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);

--
We have phasers, I vote we blast 'em!
-- Bailey, "The Corbomite Maneuver", stardate 1514.2

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 22 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.