4

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

10
  • 3
    A named object is always an lvalue. Commented Jun 6, 2012 at 16:17
  • 4
    Arrays are not pointers, yet you seem to be trying to use it as such. Commented Jun 6, 2012 at 16:18
  • 3
    There's a very thorough explanation of these things (and more) here. Commented Jun 6, 2012 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. Commented Jun 6, 2012 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". Commented Jun 6, 2012 at 16:36

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Luchian: "compile-time" in C89, not for C99 VLAs. It's always an operator, not a function.
@SteveJessop: Bang on. For VLA's standard mandates sizeof does run-time evaluation.
@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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.