AR89
AR89

Reputation: 3628

C pointers and arrays

I learned that:

char ar[] 

is the same of

char *ar

These three expressions:

char ar[][] //1
char *ar[] //2
char **ar //3

are the same thing for the compiler?

These two expressions:

char ar[]
char ar[][]

will allocate the array on the stack, while all the others will allocate it on the heap?

Upvotes: 2

Views: 234

Answers (6)

fkl
fkl

Reputation: 5535

char ar[]; creates an array of characters when size is specified.

char * ar; creates a character pointer. Might point to even a single or more characters.

char ar[][]; when size is specified, creates a 2 dimensional array.
char *ar[]; creates an array of character pointers
char **ar; a pointer to pointer to character.

When you allocate memory statically such as

char a[10]; // this goes on stack

where as

char *a = malloc(10); // this goes on heap and needs to be freed by the programmer

A perhaps common thing could be that you allocated an array of arrays using a char **a i.e. each element of a is also a character array. Then any one element of this could be accessed using the syntax a[x][y]

Yet another difference is that char *a is a pointer variable i.e. a can be reassigned to another address where as char a[] would return a pointer constant and cannot be reassigned.

Upvotes: 4

Lstor
Lstor

Reputation: 2263

The thing is that arrays contain more information than pointers. You can determine what the size of an array is, and the compiler knows. For example, the type of "Hello" is const char[6]. Notice the size. The following snippet illustrates:

#include <stdio.h>

int main()
{
    const char array[] = "Hello";
    char* pointer = "Hello";
    printf("Array: %d - Pointer: %d", sizeof(array), sizeof(pointer))
    return 0;
}

On my system, the snippet gives the following output:

Array: 6 - Pointer: 4

For arrays, it is fine for the compiler to know the size, because they are always known at compile-time. Pointers, however, are often determined at run-time, which is why they are usually associated with dynamically allocated memory.

A really simplified way of thinking about it is that arrays is the "whole thing", whereas a pointer is just a reference to the first element.

Upvotes: 0

Alexey Frunze
Alexey Frunze

Reputation: 62048

char ar[] is only the same as char *ar when ar is a function parameter. Otherwise they are an array and a pointer respectively.

char ar[][] is a 2-d array if ar is not a function parameter. Otherwise it's a pointer to a 1-d array.

char *ar[] is a 1-d array of pointers if ar is not a function parameter. Otherwise it's a pointer to a pointer.

char **ar is a pointer to a pointer.

Basically, if it's a function parameter and it looks like it's an array, it's actually a pointer to the array's first element. Arrays aren't passed in their entirety as function parameters. When you try to, you will pass pointers to first elements of the arrays, and not arrays themselves.

All variables defined outside of functions aren't neither in the heap nor on the stack. They are global variables.

All variables defined inside of functions (with the exception of static variables) are on the stack. static variables are global-ish, they aren't neither in the heap nor on the stack. static reduces the visibility of a global variable to the function or module scope, only that.

Only those variables allocated explicitly via malloc(), calloc(), ralloc() live in the heap. Some standard library functions may create variables/objects in the heap, e.g. fopen().

Upvotes: 1

Whoami
Whoami

Reputation: 14408

1) Both are not same, but while passing the array into the function, it is considered as a pointer.

2)

int ar[10][10]

ar is a two dimentional array with 10 rows, 10 columns.

char *ar[]

ar is array that contains pointers of type 'char'.

char **ar

ar is a point to pointer of type 'char'.

3)

char ar[20]
char ar[10][10]

both allocates the memory from the stack. To allocate memory from the heap, then you will have to use dynamic memory allocation concept. ie: malloc.

Hope this helps. :)

Upvotes: 1

Mike
Mike

Reputation: 49373

char ar[] and char *ar are not the same thing. One is an array, one is a pointer. If you're saying:

char ar[10] = {0};

ar[0] == *ar

Then yes, they can both be dereferenced and have the same result. Regarding heap vs. stack, that doesn't have anything to do with what you're talking about... variables are allocated off the stack when they are static:

char ar[10] = {0};  // this is allocated off the stack.

variables are allocated off the heap when they are dynamic:

char *a = malloc(10); //this is from the heap

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363517

  1. They're not the same, except in function prototypes.
  2. Again, they're not the same, except that the last two are the same in function prototypes.
  3. None of this has to do with heap vs. stack allocation.

Upvotes: 3

Related Questions