-5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int *ptr,a[6]={1,2,3,4,5,6};
    int i;    
    ptr=(int*)calloc(a[6]*sizeof(int),2);
    for(i=0;i<7;i++)
    {
        printf("\n %d",*ptr+a[i]);
    }
    free(ptr);
    getch();
}

the program works perfectly!! but my questions

  1. Does the declaration of a[6] inside calloc() correct. since the syntax for calloc is ptr_var=(cast_type *)calloc(no_of_blocks,size_of_each_block);.what does the calloc() and a[6] do now.

  2. What does *ptr takes value because ptr doesnt get assigned any address.

  3. In printf does *ptr needs there.then what does it do.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
t72belt90
  • 25
  • 5
  • 1
    This is pretty meaningless; there is no `a[6]`, and you're supply the size as part of the wrong argument to `calloc`. – Oliver Charlesworth Oct 01 '14 at 19:38
  • 2
    [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845) – Deduplicator Oct 01 '14 at 19:39
  • "the program works perfectly" - are you *sure* about that? Your code invokes *undefined behavior* in the first `calloc` invoke by dereferencing an array one-element-past its last addressable element. Your program is ill-formed. If it "works", it isn't perfectly, its by sheer luck (all of it bad). – WhozCraig Oct 01 '14 at 19:41
  • ive tried without a cast.it works on turbo c. the link ishttp://www.vdesignourweb.com/c/c_dynamic_memory.html – t72belt90 Oct 01 '14 at 19:45
  • http://www.vdesignourweb.com/c/c_dynamic_memory.html – t72belt90 Oct 01 '14 at 19:45

2 Answers2

3
  1. ptr=(int*)calloc(a[6]*sizeof(int),2); is amiss.
    The (int*) cast is not needed.
    The a[6] is the value of a[6] which is outside a valid index range of 0 ...5.
    Instead the number of elements and the size of size of *ptr are needed.
    calloc() will zero-fill the memory assigned to ptr.

    size_t n = TBD;  // Somehow determine the desired number of `int`s for `ptr`.
    ptr = calloc(n, sizeof *ptr);
    
  2. ptr does get a value. To see its value

    printf("%p\n", (void*) ptr);
    
  3. To print the values of the elements of ptr:

    size_t n =  TBD;  // n is the number of elements.
    for (size_t i=0; i<n; n++) 
      printf("%d\n", ptr[i]);
    

a does not need memory allocation as that was done in its declaration. To see its values

    for (size_t i=0; i<sizeof a/sizeof a[0]; i++) 
      printf("%d\n", a[i]);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
3
1.does the declaration of a[6] inside calloc() correct. since the syntax for calloc is 

No, its not correct. The array a has 6 elements with indices 0,1,2,3,4,5. but no 6.

2.what does *ptr takes value because ptr doesnt get assigned any address.

ptr does get an address. You are assigning address to ptr when you make the call to calloc() and store the returning address to it. As *ptr is a pointer to an int, it will take integer as a value.

3.in printf does *ptr needs there.then what does it do.

Since you have not stored any value in that allocated memory that ptr points to, it will be all 0's

Haris
  • 12,120
  • 6
  • 43
  • 70