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

plz tell me the difference

char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences

Apr 28 '06 #1
10 1657

"jeniffer" <ze******************@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences


Questions 1.32 and 6.2 and their answers should help. If not, tell us why
not.
http://www.c-faq.com
Rod Pemberton
Apr 28 '06 #2
AG
char *a declares a pointer to a string. So, to say char *a = "this is
a string", puts the address of the first letter of the string into the
pointer a. So, that first "t" is located somewhere in memory, the
pointer a contains the address.
char *a="this is a string";

If you wrote printf("char = %d",a); you would see the address
where a has been stored
If you wrote printf("char = %c",*a); you would see what value was
at that address.

flank

Apr 28 '06 #3

"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.

char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.

i hope this help you....

jeniffer wrote:
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences


Apr 28 '06 #4

"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.

char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.

i hope this help you....

jeniffer wrote:
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences


Apr 28 '06 #5
AG opined:
char *a declares a pointer to a string. So, to say char *a = "this
is a string", puts the address of the first letter of the string into
the pointer a. So, that first "t" is located somewhere in memory,
the pointer a contains the address.
So far so good, albeit somewhat less than precise...
char *a="this is a string";

If you wrote printf("char = %d",a); you would see the address
where a has been stored


No, you wouldn't.

You'd see (if you're lucky) whatever your implementation thinks a
pointer interpreted as an `int` looks like. In fact, you get Undefined
Behaviour, as "%d" tells `printf()` to expect an `int`, when in fact
you're passing `char *`. UB essentially means: all bets are off, duck
or grouse!

Also, since you didn't terminate output with '\n', you may end up
getting absolutely no output at all.

You could output a pointer (as represented by your implementation) if
you used "%p" instead. How, and whether it relates to memory
addresses, is another matter.
If you wrote printf("char = %c",*a); you would see what value
was at that address.


This will work as expected, yes (apart from missing '\n', that is).
--
The trouble with a kitten is that
When it grows up, it's always a cat
-- Ogden Nash.

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

Apr 28 '06 #6
code break opined:

"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.
C knows not of "data segment", nor does it say where exactly the string
is stored. It's important to note that you're not allow to modify it.
char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.
Wrong.

In this case `a` is not a pointer, but an array. It is correct that you
can't make `a` refer to something else, but that is true of any
variable you declare. As it is not declared `const` the contents of
this array can be modified.

Also, in the line above, the string is used to initialise array `a`
(including the terminating '\0'). It is not otherwise accessible from
the program.
i hope this help you....


Actually, no.

--
He's dead, Jim
-- McCoy, "The Devil in the Dark", stardate 3196.1

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

Apr 28 '06 #7

jeniffer wrote:
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences


I'm going to rewrite this a little:

char *ptr="this is a string";
char arr[]="this is a string";

In the above code, we're talking about 3 distinct objects: the string
literal "this is a string", the pointer ptr, and the array arr. The
following hypothetical memory map shows the state of each object after
initialization (best viewed with a fixed-size font):

Item Address Contents
---- ------- --------
literal 0x0800 "this is a string"
ptr 0x1000 0x0800
arr 0x2000 "this is a string"

The string literal has its own address in memory, and may not be
writable (i.e., you may not be allowed to write to the bytes between
0x0800 and 0x0811; assume that you can't).

The pointer ptr contains the address of the string literal. Since the
literal may not be writable, you may not write to *ptr or ptr[n];
however, you can assign a new value to ptr (i.e., ptr=NULL,
ptr=&arr[0], ptr=malloc(16), etc.), and if this new value points to
writable memory, you may then also write to *ptr or ptr[n].

The array arr contains a *copy* of the contents of the string literal.
The array contents are writable, so you can write to *arr or arr[n];
however, the array object itself is not writable, so you may not write
arr=NULL, or arr="this is a string", or arr=p, etc.

Apr 28 '06 #8
"AG" <an************@gmail.com> writes:
char *a declares a pointer to a string. So, to say char *a = "this is
a string", puts the address of the first letter of the string into the
pointer a. So, that first "t" is located somewhere in memory, the
pointer a contains the address.


Strictly speaking, char *a declares a pointer to a char, not a pointer
to a string. That pointer-to-char can, and commonly does, point to
the first character of a string (a sequence of chars terminated by a
'\0'), and can be used to access the entire string. Referring to a as
a "pointer to a string" is a common verbal shorthand, and a useful
one, but it's not literally correct.

More generally, it's common to use a pointer-to-FOO to point to the
first element of an array of FOO:

FOO arr[10];
FOO *ptr = arr; /* or, equivalently, FOO *ptr = &arr[0]; */

but it's *also* possible to have a pointer to an array, which is
distinct from a pointer to the array's first element:

FOO (*arr_ptr)[10] = &arr;

Calling ptr a "pointer to an array" blurs this distinction.

Incidentally, pointers to arrays are rarely useful. It's usually more
useful to have a pointer to the first element of an array, and keep
track of the length separately rather than hard-wiring a fixed length
into an pointer-to-array type.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 28 '06 #9
Keith Thompson wrote:

"AG" <an************@gmail.com> writes:
char *a declares a pointer to a string.
So, to say char *a = "this is a string",
puts the address of the first letter of the string into the
pointer a. So, that first "t" is located somewhere in memory, the
pointer a contains the address.


Strictly speaking, char *a declares a pointer to a char, not a pointer
to a string. That pointer-to-char can, and commonly does, point to
the first character of a string (a sequence of chars terminated by a
'\0'), and can be used to access the entire string. Referring to a as
a "pointer to a string" is a common verbal shorthand, and a useful
one, but it's not literally correct.


(a) is a "pointer to a string" according to the definition
in the standard for "pointer to a string".

I don't think it would be wrong to say that
char *a = "this is a string";
declares a pointer with a value unequal to NULL.
The point being, that there are other aspects
to an initialised declaration, besides the type.

--
pete
Apr 28 '06 #10
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
"AG" <an************@gmail.com> writes:
> char *a declares a pointer to a string.
> So, to say char *a = "this is a string",
> puts the address of the first letter of the string into the
> pointer a. So, that first "t" is located somewhere in memory, the
> pointer a contains the address.


Strictly speaking, char *a declares a pointer to a char, not a pointer
to a string. That pointer-to-char can, and commonly does, point to
the first character of a string (a sequence of chars terminated by a
'\0'), and can be used to access the entire string. Referring to a as
a "pointer to a string" is a common verbal shorthand, and a useful
one, but it's not literally correct.


(a) is a "pointer to a string" according to the definition
in the standard for "pointer to a string".


You're right. C99 7.1.1 says:

A _pointer to a string_ is a pointer to its initial (lowest
addressed) character.

If you interpret the phrase literally, it's a useful shorthand but not
strictly correct. Since the standard provides a definition for it, I
concede the point.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 28 '06 #11

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

Similar topics

6
by: Fan Ruo Xin | last post by:
Please don't tell me using DB2LOOK. I only found the following difference when I checked the SYSCAT.TABLES. tabname TYPE STATUS PROPERTY -------- ---- ------ --------------------------------...
1
by: orion30 | last post by:
I used the read function in order to read a big file. I read the file by 1024 blocks in a WHILE statement. During the read statement there is a problem. Indeed, the read function return 1023 and...
6
by: Marty | last post by:
Is there a property or method to tell you if you are running in the IDE or as a compiled exe? Thanks.
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
5
by: chenedor | last post by:
Hi all I am a bit confuse about unboxing... what is the difference and what is best practice ? object o = __box(1234); int n = *dynamic_cast<__box int*>(o); of
13
by: Jiho Han | last post by:
Here's the issue. You have a class, Class Person { public int id; public string firstname; public string lastname; }
0
by: ranilb5 | last post by:
Hi All, Can anybody please tell me if there is any considerable difference between the content of above two editions by Brian W. Kernighan, Dennis Ritchie and Dennis M. Ritchie? New edition has...
1
by: yogesh | last post by:
i have a code as follows TServerDB() { fDB = Server()->dbStorage->Create(); } or fDB=TServer->TMySQLBD->Create(); can any one tell the relationship between two and whats the <between...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
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
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.