Results 1 to 5 of 5

Thread: How to use void pointer to generate 2D Array in c++

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    How to use void pointer to generate 2D Array in c++

    Hello to all,
    I recently start learning c++ language. I tried various method but I unable to generate a 2D array using void pointer. Can anyone tell me how to use void pointer to generate 2D Array in c++. Please help me.
    I have tried following program to generate 2D Array, but it is not working properly.

    Code:
    int **k;
    int rows = 2;
    int cols = 2;
    
    k = new int* [rows];
    	
    for(int x=0; x< rows; x++)
       k[x] = new int[cols];
    
    for (int x = 0; x < rows; x++)
       for (int j = 0; y < cols; y++)
          k[x][y] = rand () % 10;

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use void pointer to generate 2D Array in c++

    Hey you have written wrong code for generating 2D Array in c++ using void pointer and that's why you are getting such type of problem. In your code you have written k = new int* instead of k = new void** and that's why you are getting such type of problem. I have written correct cod for you just try to understand it.



    Code:
    
    void*** k;
    int rows = 2;
    int cols = 2;
    
    k = new void** [row];
    	
    for(int x=0; x< row; x++)
       k[x] = new void*[cols];

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to use void pointer to generate 2D Array in c++

    It is very easy process to to generate 2D Array in c++ using void pointer. You have to just use two for loop to do this. I have written following program for you to generate 2D Array in c++. It is very simple program. In the following program I have create one pointer known as *q to generate 2D Array.

    Code:
          int*q;
    		 
    		 
    	for (int x = 0; x < rows; x++)
    	{
    		for (int y = 0; y < cols; y++)
    		{
    			q = new int;
    			*q = rand () % 10;
    			m[x][y] = q;
    		}
    	}

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to use void pointer to generate 2D Array in c++

    You can use void pointers to generate 2D Array in following ways. It is very simple program. You can declare pointer in following ways.

    int *void; //it can be treated as any other type (char, float...)
    void = new int[x];

    Code:
    int **void;
    int X,Y;
    cin >> X >> Y;
    
    void = new int*[X];
    for(int k=0;k
    Above code will create a 2D array, with XxY dimentions.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use void pointer to generate 2D Array in c++

    Void pointer is treated as some unspecified type. It can be declare as normal pointer. It has same as size as other pointer. I have written following program for you. This program generate 2D Array using void pointer. It is very simple program. Just try to understand it.

    Code:
    int **k;
    
    k = new int* [rows];     
    for(int x=0; x< rows; x++)     
        k[x] = new int[cols];
    
    for (int x = 0; x < rows; x++)
       for (int y = 0; y< cols; y++)
             k[x][y] = rand () % 10;
    
    void ***v = (void ***)k;

Similar Threads

  1. Don't know about void pointer
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 01:39 PM
  2. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM
  3. Pointer to an array of pointers
    By pushpendra in forum Software Development
    Replies: 3
    Last Post: 29-12-2009, 12:45 PM
  4. Size of a pointer to an array of char
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 12:05 PM
  5. Pointer to an array of structures
    By Zool in forum Software Development
    Replies: 3
    Last Post: 13-05-2009, 10:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,149,137.35220 seconds with 17 queries