C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Memory allocation with malloc

Posted By: Matthew Evans     Category: C Programming     Views: 10921

Write a program that uses a table of integers whose size will be specified interactively at run time.

Code for Memory allocation with malloc in C Programming

#include <stdio.h>
#include <stdlib.h>
#define NULL 0

    int *p, *table;
    int size;
    printf(“\nWhat is the size of table?”);
        /*------------Memory allocation --------------*/
if((table = (int*)malloc(size *sizeof(int))) == NULL) { printf(“No space available \n”); exit(1); } printf(“\n Address of the first byteis %u\n”, table); /* Reading table values*/
printf(“\nInput table values\n”); for (p=table; p<table + size; p++) scanf(“%d”,p); /* Printing table values in reverse order*/
for (p = table + size –1; p >= table; p --) printf(“%d is stored at address %u \n”,*p,p); } Output What is the size of the table? 5 Address of the first byteis 2262 Input table values 11 12 13 14 15 15 is stored at address 2270 14 is stored at address 2268 13 is stored at address 2266 12 is stored at address 2264 11 is stored at address 2262

Didn't find what you were looking for? Find more on Memory allocation with malloc Or get search suggestion and latest updates.

Matthew Evans
Matthew Evans author of Memory allocation with malloc is from London, United Kingdom.
View All Articles

Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

No Comment Found, Be the First to post comment!