1

First I want the user to input what is the size of the desired array. So I am using:

int size;
scanf("&d",&size);

Now I want to create an integer array using a pointer and the malloc function. This is what I did:

int *p1 = (int*)malloc(sizeof(int)*size);

According to my understanding, this is like using:

int p1[size];

But how do I use it like an array?

Question 1: Now I want the user to input as many integers as he wrote into this "array". But I can't use p[0] because it is not an array, it is a pointer.

Question 2: I want to "send" this array to a function that gets an array of integers. So again, this is not an array, how can I "give" it to the function?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
TomG
  • 2,409
  • 4
  • 23
  • 40
  • 1
    Does scanf("%d",&p1[0]); give you any errors? That should work – Lefteris E Apr 16 '13 at 11:47
  • Hint: a pointer holds an address, and its an address that `scanf()` requires for slurping data. So.. `scanf("%d", p1+n)`, where `n` is the element being read. – WhozCraig Apr 16 '13 at 12:13
  • Please note that unless your compiler is ancient, nothing is stopping you from declaring an array as `int p1[size];`, where size has a value assigned in run-time. – Lundin Apr 28 '16 at 06:20

4 Answers4

2

Answer to first question:

for(i = 0; i < size; i++ )
{
   scanf("%d",&p[i]); 
   /*p[i] is the content of element at index i and &p[i] is the address of element 
at index i */
}

Or

for(i = 0; i < size; i++ )
{
   scanf("%d",(p+i)); //here p+i is the address of element at index i
}

Answer to second question:

For sending this array to the function, just call the function like this:

function(p); //this is sending the address of first index of p

void function( int *p ) //prototype of the function
A human being
  • 1,220
  • 8
  • 18
0
  • Question 1: 1d arrays and pointers to properly allocated memory are pretty-much the same thing.
  • Question 2: When passing an array to a method you are actually passing the address of the 1st element of that array

An array is actually a pointer to the first element of the array

Lefteris E
  • 2,806
  • 1
  • 24
  • 23
  • I know it's the same thing, that's why i'm asking how can I use it like an array? let's say i want to use : scanf("%d",&p[0]); but p is a pointer, so i can't do it. right? – TomG Apr 16 '13 at 11:39
  • scanf("%d",&p1[0]); and scanf("%d",p1 + 0); should do the trick – Lefteris E Apr 16 '13 at 11:39
  • @Tom They're *not* the same thing. A fixed array is a programatic label for a memory address of the first element of said-array. A pointer is an independent *variable* that *holds* an address. In the latter, the address *value* is volatile, in the former it is *not*. `int a, *p=&a; p++;` is legal. `int a[2]; a++;` is *not*. – WhozCraig Apr 16 '13 at 12:18
0

You can use the subscript-syntax to access an element of your pointer.

p1[3] = 5; // assign 5 to the 4th element

But, this syntax is actually converted into the following

*(p1+3) = 5; // pointer-syntax

For your second question, define a function and pass a pointer

int* getarray(int* p1, size_t arraysize){ } //define the function

int* values = getarray(p1, size); // use the function
bash.d
  • 13,029
  • 3
  • 29
  • 42
0

sorry for bothering everyone but Miss Upasana was right this is the correct method for dynamic array use. After declaring ur array through malloc u can directly use it exactly as array as follows::

       for(int i = 0; i < size; i++ )
    {
       scanf("%d",p+i); 
       /*p+i denoting address of memory allocated by malloc */
    }

Second Answer: Now simply pass this address to any function use address to find values like:

function(int *p)
/* access as &p for first value &p+2 for second p+4 for third and so on*/
  • All of this is very much incorrect. I'd recommend reading up about arrays and pointers, particularly _pointer arithmetic_. – Lundin Apr 28 '16 at 06:19
  • can u tell me why it is wrong or what is wrong with it @Lundin . Because all I know keeps it right.. – Pulkit Agarwal Apr 29 '16 at 06:01
  • Because pointer arithmetic takes the pointed-at type in consideration. It does not perform arithmetic on byte level which you seem to believe. And even if it did, there is no guarantee that `int` is 2 bytes. – Lundin Apr 29 '16 at 06:11
  • @Lundin Do you mean that it is storing pointer as of another type(perhaps byte level something) which we can't use in arithmetic expressions but can print them like any other integer constant. I think pointers also takes spaces for storing addresses and if they stores in memory and can be printed or taken normally then can be calculated normally too. By the way this is working fine if you check it and one question is arising is that why are you saying that integer is two byte when we will say of unsigned/signed integer not of long int or any other..? Please explain your answer.. – Pulkit Agarwal Apr 29 '16 at 13:06
  • No I mean that `int* ptr; .... ptr + 1` means "give me address of ptr plus the size of one `int` (which can be 2 bytes or 4 bytes etc). This is called pointer arithmetic and is very fundamental stuff. Read the pointer chapter in your C book. – Lundin Apr 29 '16 at 13:12
  • I read the book already @Lundin. yes they will give ptr a space to store address of any integer type(can be of 2 or 4 byte) but address which is ptr denoting locates place of integer type(eg: int a) and a will be of 2byte definite then next address will be what ptr+2 for next two byte integer variable. I think we have misconception I am talking of space which that address is denoting and you are saying of where address is storing..?? – Pulkit Agarwal Apr 29 '16 at 13:18
  • @Lundin Now I got correct explanation and understood ur comment meanings specially ur second one.. that pointer arithmetic auto depends on its Pointed type means We don't hve to multiply any size denoting value like I did by multiplying with 2. ty for that but I wanna make u sure that pointer arithmetic happens on byte level but auto matic and integer will be of wht byte is decided by compiler. like here I used 32 bit compiler which uses 4byte integer..and yes I m correcting my mistake.. thanks again for ur valuable comment.. Now I m able to understand a strong concept of pointers.. – Pulkit Agarwal Jun 29 '16 at 16:20