3

Possible Duplicate:
Is array name a pointer in C?

#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char *b=(char*)malloc(sizeof(char)*50);
    b=(char*)"hello world";
    // works

    char a[50];
    a=(char*)"hello world";
    //doesn't work. why? I thought array names are just pointers that point
    //to the first element of the array (which is char). so isn't a char*?
    return 0;
}

I think the reason it doesn't work is because there's no variable called "a" that actually stores a char* value. so should 'a' be considered an rvalue? I'm not sure if I'm understanding the concept correctly

Community
  • 1
  • 1
UXkQEZ7
  • 1,114
  • 1
  • 14
  • 24
  • 3
    A named object is always an lvalue. – R. Martinho Fernandes Jun 06 '12 at 16:17
  • 4
    Arrays are not pointers, yet you seem to be trying to use it as such. – chris Jun 06 '12 at 16:18
  • 3
    There's a very thorough explanation of these things (and more) [here](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – R. Martinho Fernandes Jun 06 '12 at 16:19
  • 1
    Be aware that your first example compiles, but it doesn't "work"! It doesn't copy "hello world" into the allocated space, it just changes `b` to point to the "hello world" string. – TonyK Jun 06 '12 at 16:23
  • Warning: the tags affect the answers. For example, Als answers for C when he says that passing an array name to a function causes decay to pointer (it doesn't in C++ for a function that takes a reference-to-array). But "rvalue" is a term from C++, not from C, which instead says "the value of an expression". – Steve Jessop Jun 06 '12 at 16:36
  • @SteveJessop: AFAIK, Even C++11 abandons "r-value" for "the value of an expression". – Alok Save Jun 06 '12 at 17:56
  • @Als: No, C++11 has `rvalues`, `lvalues`, `xvalues`, `prvalues`, and... some other type. – Mooing Duck Jun 06 '12 at 18:30
  • @MooingDuck: I look back at my comment now and yes you are correct, but i have seen this change made in some context...I can't remember exactly where :( – Alok Save Jun 07 '12 at 02:57
  • @Als: C99 does mention rvalues once, in a footnote, to say something like "the things that some people call rvalues, we call the value of an expression". Maybe that's what you remember. – Steve Jessop Jun 07 '12 at 14:31
  • @SteveJessop: Aha, you are a mind reader! Indeed true :) – Alok Save Jun 07 '12 at 18:28

3 Answers3

8

Arrays are not pointers, sometimes[Note 1:] the name of an array decays to a pointer when array name is not valid(eg: passing to function).
Arrays are non modifiable l-values, they cannot be assigned and there address can be taken.

[Note 1:]
For example:
Array name doesn't decay to a pointer when used in sizeof()

Array address cannot be changed but content can be changed.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    @Luchian: "compile-time" in C89, not for C99 VLAs. It's always an operator, not a function. – Steve Jessop Jun 06 '12 at 16:39
  • @SteveJessop: Bang on. For VLA's standard mandates `sizeof` does run-time evaluation. – Alok Save Jun 06 '12 at 16:42
  • @AlokSave _Arrays are non modifiable l-values, they cannot be assigned_ -- generally true but there's an exception to this rule too: `char const str[] = "this will be copied";` The string literal is a lvalue `char` array that is copied to `str`. – legends2k Jul 17 '14 at 09:23
5

Array is a non-modifiable lvalue. You cannot assign anything to it, yet you can apply the unary & operator to it.

You are right when you say that there's no char * variable involved here. Array name directly refers to an array object - a continuous block of memory whose size is equal to the product of the element count and element size.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
4

I thought array names are just pointers

No they're not. They're arrays. The decay into pointers when you pass them as parameters, but that's about it. You can't re-assign an array, you can only change individual values.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625