473,395 Members | 1,938 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,395 software developers and data experts.

why is this assignment correct ?

Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";
printf("%s",ptr);
return (0);
}

the output was :
possible

now how was this done? i did not assign memory to ptr ! please
explain...

sumit

Dec 10 '05 #1
9 1496
Hi sumit.
The Pointers are special type of variables. Compilers allocates some
memory for it in a particular address space so even though you dont
assign any address if can contain values.
as int a=20;
here you dont assign any memory for 'a' compiler does it. similarly
char *ptr ;
is assigned a address. Remember The Pointers are special type of
variables so like variables they also need some space in memory.
in your case char* ptr ; is a pointer which points to char type of
values and can hold char values in a particular memory space which is
assigned by compilers.
I think i am right to best of my knowledge, but still i have doubt,
which you posted. if find more info please send me.
regards
Nagaraj

Dec 10 '05 #2
A char pointer can be used for character string.

You can understand as you use malloc() to assign memory yo it.

Dec 10 '05 #3
su*******@rediffmail.com wrote:
Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";
printf("%s",ptr);
return (0);
}

the output was :
possible

now how was this done? i did not assign memory to ptr ! please
explain...

sumit


The compiler allocates the memory used by the string literal "possible",
which is a plain character array. If you now assign this array to a
variable of type pointer-to-char, it decomposes to a pointer. Your
pointer ptr now points to the first element of the string literal and
can be used in the printf() call (where the array would be decomposed as
well).

Marc Thrun

PS: Feel free to correct me if I'm wrong :-)
Dec 10 '05 #4
On Sat, 10 Dec 2005 00:26:40 -0800, sumit1680 wrote:
Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";
ptr now points to a constant string, to wit "possible". Memory for it
is allocated by the compiler, you don't need to allocate memory
for it manually.
printf("%s",ptr);
return (0);
}

the output was :
possible
I would have been suprised if it wasn't.
now how was this done? i did not assign memory to ptr !
please explain...


There's little to explain.

Dec 10 '05 #5
su*******@rediffmail.com a écrit :
int main ()
{
char *ptr = "possible";
"possible" is a string literal. It's a non modifiable static object with
an address and a permanent lifetime.

This object is actually an array of char initialized with a sequence of
characters terminated by a 0.

{'p','o','s','s','i','b','l','e',0}

Like any array, its address is the same value and type that the address
of the first element of this array, making the assignement to a pointer
to char possible.

Note that the pointed object being not mutable, it is recommended to
define the pointer with the const qualifier:

char const *ptr = "possible";

Note also that these are valid too:

char *ptr;
<...>
ptr = "possible";

char *ptr = NULL;
<...>
ptr = "possible";

char *ptr = "???";
<...>
ptr = "possible";
now how was this done? i did not assign memory to ptr !


This is misleading. You don't "assign memory to ptr". You assign an
address to a pointer. This address can be the one of any kind of object
(pointer to an object) or function (pointer to a function).

--
A+

Emmanuel Delahaye
Dec 10 '05 #6
the name itself of a character array gives its base address.
i.e suppose char name[]="arun";then the variable 'name' gives the
address of name[0] .here by initialising a pointer 'content' as
'possible' u r basically defining a character array itself.so its just
like using a normal character array.

Dec 10 '05 #7
On Sat, 10 Dec 2005 10:22:18 +0000, Kleuskes & Moos
<t.******@achter.de.del> wrote in comp.lang.c:
On Sat, 10 Dec 2005 00:26:40 -0800, sumit1680 wrote:
Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";


ptr now points to a constant string, to wit "possible". Memory for it
is allocated by the compiler, you don't need to allocate memory
for it manually.


The C standard specifies that the type of a string literal is "array
of char" and most specifically not "array of const char". Attempting
to modify a string literal in C produces undefined behavior, but this
is not because the string literal has the "array of const char", but
because the C standard specifically states that it causes undefined
behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 11 '05 #8

su*******@rediffmail.com wrote:
Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";
printf("%s",ptr);
return (0);
}

the output was :
possible

now how was this done? i did not assign memory to ptr ! please
explain...

sumit

I think the question sumit is asking is.........
usually pointer points to the address of a variable i.e int i =9; int
*ptr; ptr=&i(here we assigned the address); printf("%d",*ptr); gives
output : 9

In the same way in the sumit program we hav not at all assigned the
pointer the address of a variable,then how come the output is "
possible".

even i have a doubt in this.pls help

Dec 14 '05 #9
raghu wrote:
su*******@rediffmail.com wrote:
Hi all,

recently i tried the following peice of code in gcc

int main ()
{
char *ptr = "possible";
printf("%s",ptr);
return (0);
}

the output was :
possible

now how was this done? i did not assign memory to ptr ! please
explain...

sumit

I think the question sumit is asking is.........
usually pointer points to the address of a variable i.e int i =9; int
*ptr; ptr=&i(here we assigned the address); printf("%d",*ptr); gives
output : 9

In the same way in the sumit program we hav not at all assigned the
pointer the address of a variable,then how come the output is "
possible".

even i have a doubt in this.pls help


Why do you think it is not assigned an address? Where do you think the
string "possible" is stored.. on paper? Of course the string "possible"
is stored somewhere in memory. Exactly where depends on OS, CPU,
compiler etc.. etc... But when you type:

char *ptr = "possible";

The pointer ptr is pointing to where the string possible is stored
(actually where the first byte is stored). What's so hard to
understand.

A few things to note. String literals are read-only (don't remember the
correct C term). You are not supposed to write to a string literal. So:

x = *ptr;
y = ptr[3];

are legal, but:

*ptr = 'X';
ptr[3] = 'Y';

are not legal.

The second thing to remember is that although ptr points to where the
string "possible" is stored it is a regualr char pointer and may point
to anything else. But when you do this, remember to have something else
point to the string literal or you'll never be able to point anything
to it ever again. For example:

char *ptr = "possible";
ptr = some_other_byte_array;
/* at this point, nothing can point to the string "possible" */

Dec 14 '05 #10

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

Similar topics

4
by: Alfonzo Morra | last post by:
I've the ff code in cp assignmenent cstor: PB& PB::operator=( const PB& b) { if ( this != &b ) { PB *pb = new PB( b ) ; this = pb ; // <- Compiler barfs here } return *this ; }
14
by: ozbear | last post by:
Someone was asking in another forum how to translate: if (x && (y1=(y+y1)/2)) /* all integers with defined values */ to another language. I am at odds with myself as to whether this causes...
20
by: Michael | last post by:
Hi there, I've got a simple (newbee)-question. Is the following thing right? struct x { int a; int b; } ...
6
by: Sweety | last post by:
#include<stdio.h> void main() { int a=0,b=6,c=7,d=8,e; e = a ? c , b : c , d; printf("%d",e); } o/p->7
8
by: sacgarg | last post by:
I was searching a topic on "Why assignment operator does not return by value" but I found no where in any book or on any web site. According to my unserstanding, return by value is logically...
5
by: Tom Smith | last post by:
I'm writing my own smart pointer class (as an exercise, not for real life use); I've got it to a point where I think it's usable: but I'm not quite sure. Any comments on the class gratefully...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
18
by: digz | last post by:
Hi , I am trying to write a class that encapsulates a Union within it , I intend the code to be exception safe. can someone please review it and tell me if I have done the right thing , are...
6
by: jt | last post by:
#include <stdio.h> void f(); int main() { long int i; i=20; f(); i = 10; printf ("\n%d\n",i);
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.