Search
 
SCRIPT & CODE EXAMPLE
 

CPP

malloc 2d array cpp

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int r = 3, c = 4;
 
    int* ptr = malloc((r * c) * sizeof(int));
 
    /* Putting 1 to 12 in the 1D array in a sequence */
    for (int i = 0; i < r * c; i++)
        ptr[i] = i + 1;
 
    /* Accessing the array values as if it was a 2D array */
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++)
            printf("%d ", ptr[i * c + j]);
        printf("
");
    }
 
    free(ptr);
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ last element of vector 
Cpp :: c++ structs 
Cpp :: file streams in c++ 
Cpp :: C++ insert character 
Cpp :: deque 
Cpp :: queue cpp 
Cpp :: remove duplicates from sorted list solution in c++ 
Cpp :: c++ add everything in a vector 
Cpp :: check if cin got the wrong type 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: using-controller-and-qt-worker-in-a-working-gui-example 
Cpp :: Common elements gfg in c++ 
Cpp :: graph colouring 
Cpp :: c++ friend keyword 
Cpp :: return multiple values c++ 
Cpp :: binary to int c++ bitset 
Cpp :: reverse the number codechef solution in c++ 
Cpp :: setFontSize QT 
Cpp :: what does map.count() return in c++ 
Cpp :: c++ if 
Cpp :: TCA9548 I2CScanner Arduino 
Cpp :: dinamic 
Cpp :: unions c++ 
Cpp :: a suprise... c++ 
Cpp :: KUNG FU HUSTLE 
Cpp :: C++ References 
Cpp :: cannot access base class members 
Cpp :: run program until ctrl-d c++ 
Cpp :: Implement a currency converter which ask the user to enter value in Pak Rupees and convert in following: in cpp 
Cpp :: all in one c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =