Member Avatar for Rahul47

I have two questions:
1)
Here is a small program showing how one can use Array name as Pointer variable.

#include<stdio.h>

int main(){
    int a[5]={1,2,3,4,5},i=0;
    for(i=0; i<5; i++){
        printf("\nAddress: %d\tValue: %d", a+i,*(a+i)); 
    }
    getch();
    return 0;
}

If we can use array name as pointer variable, then why separately declare pointer variable ?

2) Is there anything like Pointer Constant as Pointer Variable ?

I am not sure exactly what you mean by #2, but I can tell you about #1. The difference is that, as far as I know, you cannot create an array variable without setting it to point to a value. Specifically when you say int a[5]={1,2,3,4,5}; it sets a to point to the address where the 1 is stored, while ensuring that 2,3,4,5 are in the following address spaces. If you were to then move where a points you would have an issue because you would lose access to your original array (and I am not sure if C would clean it up for you). However, in the context of function parameters arrays and pointers can be used almost interchangeably. For example:

void arrayTest(int a[])
{
    a[0]=7;
}

Is the same as:

void pointerTest(int *a)
{
    *a=7;
}

Just like:

int arrayAccess(int a[])
{
    return a[7];
}

Is the same as:

int pointerAccess(int *a)
{
    return *(a+7);
}

And of course a[7] and *(a+7) can be interchanged.

So the reason we declare pointers seperately is two-fold:

1) It doesn't automatically create the object we wish to point to.
2) It describes what the variable is, making code more legible.

I believe Labdabeta answered the first question well. As for your second question you can technically do this...

const *p = (const *)0x05;  // Constant pointer address
char *a = (char *)p;       // Assign this address to a character array
printf("%p", a);           // View the address of the character array

This will print out "0x5".

It is not advisable to work with pointers this way unless you are certain of the memory mapping. While programming in C you essentially give up control of the memory mapping and let C and the Operating System deal with that for you.

Of course if you are certain of the memory mapping (such as with embeded devices or OS programming) by all means create constants for these "POKE"-style addresses. This is usually done as a series of #define lines in headers.

commented: +1 for answering the second part. +4

An arry variable does not "point" at anything, it is an array with assigned memory and the properties of an array. On of the properties of an array is that when its symbol is used without an index it decays to a pointer to the first item in the array. Another property of arrays is that you can't pass arrays by value, that is you can not pass an array to any function, you can only pass a pointer to the first item in the array (what is most commonly done along with the size of the array) or a pointer to the array itself.

void fn1(char *p)
{
  printf("fn1: %d %d\n", sizeof(p), sizeof(*p));
}

void fn2(char p[])
{
  printf("fn2: %d %d\n", sizeof(p), sizeof(*p));
}

void fn3(char p[5])
{
  printf("fn3: %d %d\n", sizeof(p), sizeof(*p));
}

void fn4(char (*p)[5])
{
  printf("fn5: %d %d\n", sizeof(p), sizeof(*p));
}

In fn1, fn2 and fn3 p has type char* because it is just a char pointer and the values printed are 4(because I am using a 32 bit system) and 1. in fn4 p has type char (*)[5] and sizeof p is 4 still, but sizeof *p is now 5 because p points to an array.

The point is that in a function parameter using [] instead of * means nothing to the compiler although programmers sometimes use the [] notation to indicate the function is expecting an array rather than a pointer to a single thing. Also note that there is no notation for passing an array to a function by value fn3 which seems to do this just produces a pointer.

An array can also not be the return value of a function.

An array is not a pointer, although it can be decayed to a pointer and a pointer is definately not an array. This all becomes rather more obvious if you start working with an array of arrays char array[5][5]; instead of just a straight array e.g. array of char.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.