String function problem

V

Vijay

Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
 
W

Walter Roberson

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

rahul

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

Jens Thoms Toerring

rahul said:
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
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top