3

In book Head First C, it says that the computer does not allocate memory for variable name of an array. I got confused! Is this really true?

But if it has a memory, why can I not assign another array to this one?

7
  • Are you talking about the name of the array or the contents of the array. The name is not allocated space, just the contents. Commented Aug 12, 2012 at 3:35
  • @Hogan But there is a mapping somewhere (in the compiler) that does take up space though... Commented Aug 12, 2012 at 3:36
  • Can you show an example of what you want to accomplish? In C you can have a pointer to an array and assign to that pointer the address of any array you want. The thing you need to remember is that in once your program is compiled into machine instructions, the computers instructions no longer deal with the actual names that you gave to your variables. Commented Aug 12, 2012 at 3:36
  • 4
    Whoever -1'ed this question should post the reason to help the person asking the question improve their question. Commented Aug 12, 2012 at 3:37
  • @jsn - I think it is a safe assumption that an intro book to C code is not talking about writing compilers.... but I've been wrong before. Commented Aug 12, 2012 at 3:38

2 Answers 2

4

A computer program is an abstraction which describes the functionality you wish to perform. A compiler takes your description and translates it into machine code for the computer to execute.

A metaphor:

I tell you "Walk two blocks and turn left walk one block". You can then get to the store. I could also have said "Walk till you see the blue building on the left, turn 270 degrees and walk till you see the store". In both cases you will do the same thing but the instructions (program in this metaphor) are the totally different, have a different number of characters and verbs, etc.

A computer program in C is the same -- it does not matter what you name the variables -- the code that the computer will actually run will take up the same space if you call a variable "a" or if you call the variable "aVeryLongVariableName". The compiler will keep track of the names but the final output will be the same.

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

2 Comments

That's true for C. It's not true for all languages, particularly for interpreted languages that don't have separate compilation and execution phases.
@KeithThompson - I considered adding a section about interpreted languages, but as I said in the other comment, we are trying to clear up Hata's confusion, not get so technical we just end up mystifying him. Also remember: a decent interpreted language would use name table hashing anyway so size really wouldn't matter there either.
0

The identifier you are using to name an array is a pointer which actually holds the base address(Address Of First Element) of that array .

So, you can assign this address to another pointer , but assigning it to another arrays base address is not legal

1 Comment

Please don't capitalize Every Single Word. I fixed your post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.