Why the program is running without line 14?

#include<stdio.h>
#include<iostream>
//#include<conio.h>
//#include<math.h>

#define FOR(i,a,b) for(int i=a;i<b;i++)

using namespace std;

int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int *b;
//b=(int *) malloc(sizeof(a));
b=a;
//cout<<b;
FOR(i,0,10) cout<<b[i]<<" ";
//cout<<sizeof(b);

//getch();
return 0;
}

Edited 4 Years Ago by pyTony: code dropped, reinserted back, sorry!

int a[]={1,2,3,4,5,6,7,8,9,10};

You allocate memmory on stack here and a points to that memmory.
Then pointer b points to that memmory location b=a
i mean now b and a point to same location so you can work with it same as with a

here you can try to print it's addresses to check :)

#include <stdio.h>

int main()
{
   char pcArray[3] = {0};
   char *pcToArray = pcArray;
   printf("pcArray == %x and pcToArray == %x",pcArray,pcToArray);
   return 0;
}

Edited 4 Years Ago by Sokurenko: more

#define FOR(i,a,b) for(int i=a;i<b;i++)

Why? What's wrong with just using the for statement? Making it a define simply makes the program confusing.

@sokurenko...how can we use a pointer without allocating memory to it?

@waltp..sorry for the inconvenience..actually i have a file in which i have defined all the header file and macros. i just copied all these and make a new program.i actually delete most of them here.

@priya..the question is simple. i have to copy the contents of array a to b without using any loop or inbuit function

#include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<conio.h>
    #include<math.h>

    #define FOR(i,a,b) for(int i=a;i<b;i++)

    using namespace std;

    int main()
    {
    int a[]={1,2,3,4,5,6,7,8,9,10};
    int *b;
    //b=(int *) malloc(sizeof(a));
    //b=a;
    memcpy(b,a,sizeof(a));
    //cout<<b;
    FOR(i,0,10) cout<<b[i]<<" ";
    //cout<<sizeof(b);


    getch();
    return 0;
    }

now i am using memcpy function to copy the bytes. but here i require to allocate memory to b. why is so?

Edited 4 Years Ago by shanki himanshu

but here i require to allocate memory to b. why is so?

Where did you think the memory would come from? Pointers don't magically point to infinite memory on declaration, you have to point them to a block of memory that's allocated for that purpose, either through dynamic allocation (malloc and friends) or by assigning the address of an existing object.

@deceptikon..then why dont the first code is working without allocating the memory?

my basic ques is do we need to allocate memory to a pointer before using it? i mean in my first code why dont we malloc 'b' before the (b=a) statement?

@deceptikon..then why dont the first code is working without allocating the memory?

The pointer is being assigned the address of an existing array.

my basic ques is do we need to allocate memory to a pointer before using it?

You need to ensure that the pointer points to memory that you own.

i mean in my first code why dont we malloc 'b' before the (b=a) statement?

Because that's unnecessary and would cause a memory leak. The b = a statement is sufficient to meet the requirement of pointing to memory that you own.

This question has already been answered. Start a new discussion instead.