Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. how to allocate memory dynamically for 2 dimensional array and use that pointer like and array....

how to allocate memory dynamically for 2 dimensional array and use that pointer like and array....

Scheduled Pinned Locked Moved ATL / WTL / STL
linuxdata-structuresperformancehelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cmaheshwari16
    wrote on last edited by
    #1

    Hi All, I want to declare memory dynamically for a 2 dimensional array. and then want to use that pointer like an array. as i am using ANSI compiler so there is no need to typecast the return type of the memory block returned by malloc() so if i will write the inline code line as arr=malloc (nrows*5*sizeof(int)); its working correctly but i just want to know to be at the safer side that if i will be using the old compiler how to type cast the same as arr=(int [] *)(malloc (nrows*5*sizeof(int))); this is giving error.(i am on linux) thanks

    #include
    #include
    int main()
    {
    int nrows=2,i,j;
    //int arr[nrows][5];
    int (*arr)[5];
    arr=(int [] *)(malloc (nrows*5*sizeof(int)));
    for(i=0;i

    S 1 Reply Last reply
    0
    • C cmaheshwari16

      Hi All, I want to declare memory dynamically for a 2 dimensional array. and then want to use that pointer like an array. as i am using ANSI compiler so there is no need to typecast the return type of the memory block returned by malloc() so if i will write the inline code line as arr=malloc (nrows*5*sizeof(int)); its working correctly but i just want to know to be at the safer side that if i will be using the old compiler how to type cast the same as arr=(int [] *)(malloc (nrows*5*sizeof(int))); this is giving error.(i am on linux) thanks

      #include
      #include
      int main()
      {
      int nrows=2,i,j;
      //int arr[nrows][5];
      int (*arr)[5];
      arr=(int [] *)(malloc (nrows*5*sizeof(int)));
      for(i=0;i

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Try this:

      arr=(int (*)[5])malloc (nrows*5*sizeof(int));

      Alternatively (and this is clearer overall) use a typedef:

      typedef int (*ArrType)[5];

      int main()
      {
      ArrType arr;
      arr=(ArrType)malloc (nrows*5*sizeof(int));
      ... Rest of code ...
      }

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups