0

Are arrays in C Language actually Pointers? Why not? in C++?

Can a pointer in Normal C/C++(Programming) be used to dereference any amount of value? Doesn't it have any limits to the memory it is pointing to? Suppose if I declare char *ptr. Then I can dereference a single character. And a 1000 character string too? Any number of strings? (Why there is no limitation)?

Is it same with C And C++ Programming? If not, then why not?

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
Avan
  • 366
  • 3
  • 17
  • 2
    [No, they aren't.](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – chris Aug 09 '14 at 01:53
  • When we declare char c[30]..in C Programming. We are actually also creating a constant pointer . – Avan Aug 09 '14 at 01:54
  • 3
    No, you aren't. You are creating an array of thirty characters. Nowhere in there is a pointer. If it's declared as a function parameter, the type will be adjusted to `char *`, but otherwise, it's an array. – chris Aug 09 '14 at 01:59
  • 4
    @Avan: No, we are not. Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). Briefly, the expression `c` is implicitly *converted* to a pointer to (i.e., the address of) its first element in most contexts, but `char c[30];` does not create a pointer *object*. – Keith Thompson Aug 09 '14 at 01:59
  • 2
    A `char*` pointer can point to a single `char` object. Using pointer arithmetic, we can also access more `char` objects stored in memory adjacent to the one that's pointed to directly. Array indexing is defined in terms of pointer arithmetic. – Keith Thompson Aug 09 '14 at 02:01
  • Looks like my dilemma is solved .here. Thank you. Chris & Keith Thompson. – Avan Aug 09 '14 at 02:07

4 Answers4

4

Arrays in C are not pointers. Arrays are [potentially large] contiguous blocks of memory - objects consisting of many smaller objects, called array elements.

As for "why not"... Arrays used to be pointers in B and BCPL languages - C's predecessors and forefathers. They were pointers in the very first sketchy version of C - "embryonic" C. However, C language wanted to introduce a concept of aggregate struct types. If arrays were implemented as pointers in C, then struct objects containing arrays would require non-trivial initialization ("construction") and would become non-copyable (even with memcpy). That was considered unacceptable. This is what led to the redesign of the idea of an array in C. You can read about it on Dennis Ritchie's The Development of the C Language page (see "Embryonic C" section).

As for how much memory you can access through a pointer... Read about pointer arithmetic.

Raw arrays in C++ are virtually the same as they are in C.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • "Raw arrays in C++ are exactly the same as they are in C" - that is unfortunately not entirely true: C allows arrays to be of dynamic size, you can even typedef a pointer to an array of dynamic size (`int size = foo(); typedef int (*myPtr)[size];` is perfectly legal and well defined). This is not possible in C++. C++ restricts all array types to compile time sizes. – cmaster - reinstate monica Aug 09 '14 at 08:12
  • @cmaster: Yes, that's true. There's are other differences between even C89/90 arrays and C++ arrays as well. However, one might argue that VLAs are beasts of completely different nature. In any case, ,y point is that even VLAs are not pointers at conceptual level (even though they are implemented through pointers internally). – AnT stands with Russia Aug 09 '14 at 16:05
1

The amount of memory a pointer can point to is limited only by the amount of memory in the computer. It only points to where a particular item starts; it says nothing about how much follows. Actually, it does carry some information about the size of the first object it points to, but not about how many of those objects follow.

Logicrat
  • 4,438
  • 16
  • 22
  • That implies we can redirect a pointer to any amount of memory. – Avan Aug 09 '14 at 02:12
  • Actually, it can point to any amount of memory that a process has. A rather subtle (and in this day and age maybe a meaning) difference. Also, I do have anecdotal evidence that even on 64-bit machines with 24 Gbytes of ram mallocs or news over 4Gbytes fails. Granted I may have missed a flag for really large allocations or something -- I didn't search really hard for a solution, just stopped being lazy and worked out a more efficient method. – thurizas Aug 09 '14 at 03:19
  • No, a pointer does not carry any information about the size of the first object it points to ... that information only exists in the code that processes the memory that the pointer points to. – Jim Balter Aug 09 '14 at 03:45
  • @JimBalter To be really precise, it is the type of the pointer that carries the size information. *And that size may be either a compile time or a runtime constant!* Here is an example: `int size = foo(); int (*bar)[size];` In this example (legal C, not C++), the size of the thing that `bar` points to is not known at compile time, yet it is a part of the type, and will be used when pointer arithmetic is performed on `bar`, i. e. `bar[3][5]` will access an integer at offset `3*size + 5`. – cmaster - reinstate monica Aug 09 '14 at 08:06
  • @cmaster Yes, of course it is the type that determines the size ... which isn't inconsistent with what I wrote. Even in the case of VLA, the dynamic size of the array is not carried by the pointer. – Jim Balter Aug 09 '14 at 10:25
0

If you point your index finger at a house, does that make the house a finger of your hand?

Is there a limit to the set of things you can point your index finger at?

(Yes, the analogy only goes so far, but that's what you have the other proper answers for.)

Lumi
  • 14,775
  • 8
  • 59
  • 92
0

Arrays are not pointers.


  • int arr[10]:

    • Amount of memory used is sizeof(int)*10 bytes

    • The values of arr and &arr are necessarily identical

    • arr points to a valid memory address, but cannot be set to point to another memory address


  • int* ptr = new int[10]:

    • Amount of memory used is sizeof(int*) + sizeof(int)*10 bytes

    • The values of ptr and &ptr are not necessarily identical (in fact, they are mostly different)

    • ptr can be set to point to anything (valid and invalid memory addresses) as much as you will

barak manos
  • 29,648
  • 10
  • 62
  • 114