Created
November 20, 2015 11:24
-
-
Save serhii-shnurenko/fb8f69501a77cec84203 to your computer and use it in GitHub Desktop.
Allocating in memory piece which later will be used as array using C programming language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//STUDYING MATERIAL | |
#include <stdio.h> | |
int main() | |
{ | |
const int WIDTH=100; | |
const int HEIGHT=100; | |
int a[HEIGHT][WIDTH]; | |
int i; | |
for(i=0;i<HEIGHT;i++){ | |
int j; | |
for(j=0;j<WIDTH;j++){ | |
a[i][j] = i*WIDTH+j; | |
} | |
} | |
int* ptr = &a; | |
for(i=0;i<HEIGHT;i++){ | |
int j; | |
for(j=0;j<WIDTH;j++){ | |
printf("%d:%d\t\t%d\n",i,j,*ptr); | |
ptr++; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//STUDYING MATERIAL | |
#include <stdio.h> | |
int main() | |
{ | |
//something like user inputing dimensions | |
int width = 1000; | |
int height = 1000; | |
//allocating memory for array | |
int* array = malloc(width*height*sizeof(int)); | |
int i; | |
//moving ptr to the begining of array | |
int* ptr=array; | |
for(i=0;i<height;i++){ | |
//row by row | |
int j=0; | |
for(j;j<width;j++){ | |
//col by col | |
//writing value at current position | |
*ptr = i*width+j; | |
//moving pointer 4 bytes forward | |
ptr++; | |
} | |
} | |
ptr = array; | |
for(i=0;i<height;i++){ | |
//row by row | |
int j; | |
for(j=0;j<width;j++){ | |
//col by col | |
int a = *ptr; | |
ptr++; | |
printf("%d:%d - %d\n",i,j,a); | |
} | |
printf("\n"); | |
} | |
free(array); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment