473,395 Members | 1,341 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.

String function problem

Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
Jun 27 '08 #1
6 1521
In article <f9**********************************@p39g2000prm. googlegroups.com>,
Vijay <k.********@yahoo.comwrote:
>Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
char nm[10]

declares that nm is a variable in which 10 char can be stored.
Space for those 10 characters will be automatically allocated.
You can "really" store characters in the array nm.
char *nm

declares that nm is a variable which can be set to point
to a character and then used to access characters pointed to.
But the only space allocated by the statement is space for the
pointer itself, and that pointer is not initialized to anything
in particular, so until you cause nm to point to some real storage,
this nm cannot be used to access anything.
The above analysis does not apply if these are found in parameter
lists in function declarations: in parameter lists, there is no
difference in meaning, with both meaning that nm is of type
pointer to char.
--
"There's no term to the work of a scientist." -- Walter Reisch
Jun 27 '08 #2
Vijay <k.********@yahoo.comwrites:
Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 6 covers
arrays and pointers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.comwrote:
Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
char foo[10] defines an array of 10 characters. char *bar defines bar
as a pointer to a character.
Array name without any indexes is the pointer to the first element of
the array. foo points to the first
element i.e foo == &foo[0]. When passed to a function, the array
decays to pointer to first element.
bar can be dynamically allocated memory to contain more than one
characters.
The difference between foo and bar is foo is address of linearly
allocated memory while bar, after mem allocation,
contains the address of linearly allocated memory.
Jun 27 '08 #4
rahul wrote:
Array name without any indexes is the pointer to the first element of
the array.
There's three exceptions to that:

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue. If the array object has
register storage class, the behavior is undefined.

--
pete
Jun 27 '08 #5
rahul <ra*********@gmail.comwrote:
On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.comwrote:
Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
char foo[10] defines an array of 10 characters. char *bar defines bar
as a pointer to a character.
Array name without any indexes is the pointer to the first element of
the array.
Not really, an unadored array name stands for (not is) a pointer
to the first element of the array in a context where a value is
required. For example in

int a[10];
int *b = malloc( 100 );

a = b;

'a' is not a pointer. First reason is that 'a' appears not in
a position where a value is allowed. Here you need something
you can assign a value to. Second, if 'a' would be a pointer
the assignment would be correct (you can assign a new value
to a pointer) and you could actually change 'a' by assigning
it a new value and suddenly '*a' would be the first value in
the memory 'b' points to while 'a[0]' would still be the value
of the first element of the array. Only someone with a strong
interest in the obfuscated C contest (or a masochist) would
truely enjoy that, I guess;-)

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #6
Jens Thoms Toerring wrote:
rahul <ra*********@gmail.comwrote:
>On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.comwrote:
>>Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
>char foo[10] defines an array of 10 characters. char *bar defines bar
as a pointer to a character.
>Array name without any indexes is the pointer to the first element of
the array.

Not really, an unadored array name stands for (not is) a pointer
to the first element of the array in a context where a value is
required.
That's not the rule.
The implicit conversion is specified
as taking place always, with three exceptions.
For example in

int a[10];
int *b = malloc( 100 );

a = b;

'a' is not a pointer.
(a) is converted to a pointer type in that context.
First reason is that 'a' appears not in
a position where a value is allowed. Here you need something
you can assign a value to. Second, if 'a' would be a pointer
the assignment would be correct (you can assign a new value
to a pointer) and you could actually change 'a' by assigning
it a new value and suddenly '*a' would be the first value in
the memory 'b' points to while 'a[0]' would still be the value
of the first element of the array.
That's wrong.
The standard explicitly states that an array type is not a
"modifiable lvalue", however, even if it didn't:
The conversion of the array type expression to a pointer type,
makes the resulting expression not an lvalue,
which is sufficient to disallow an array type from appearing
as a left assignment operand.
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.

6.5.16 Assignment operators

[#2] An assignment operator shall have a modifiable lvalue
as its left operand.
--
pete
Jun 27 '08 #7

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

Similar topics

5
by: gtz669 | last post by:
I have a class which I am using for data stroage. I declare an instance of that class in my main class which is running my java applet. I Iassign it a value in the init () function and it works...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
9
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
9
by: Ronald Fischer | last post by:
Assume the following JavaScript function: function bracketize(s) { return ''; } This function which doesn't assume anything about its argument except that it must be convertible to a string.
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
5
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
8
by: Jeremy Kitchen | last post by:
I have encoded a string into Base64 for the purpose of encryption. I then later decrypted it and converted it back from Base64 the final string returns with four nothing characters. "pass" what...
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: 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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.