Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save englandbaron/2801c8d635f3c29eaa928dd083644afc to your computer and use it in GitHub Desktop.
Save englandbaron/2801c8d635f3c29eaa928dd083644afc to your computer and use it in GitHub Desktop.
malloc study
#include <stdio.h>
#include <stdlib.h>
void ChangeValue(int *pointer_array, int mos, int new_value){
*(pointer_array+mos) = new_value;
}
int main()
{
int n = 3;
int i, *ptr, sum = 0;
ptr = (int*) malloc(n * sizeof(int));
for(i = 0; i < n; ++i)
{
*(ptr+i) = i;
printf("*(ptr+i) is: %d | *(ptr) is: %d | ptr is: %p\n",*(ptr+i),*ptr,ptr);
printf("ptr+i is: %p\n================================================\n",(ptr + i));
ChangeValue(ptr,i,i*2);
}
for(i = 0; i < n; ++i)
{
printf("*(ptr+i) is: %d | *(ptr) is: %d | ptr is: %p\n",*(ptr+i),*ptr,ptr);
}
free(ptr);
return 0;
}
/*
int main()
{
int a[5]={1,2,3,4,5};
int *p;
p=(int *)(&a+1);
printf("%d,%d ",*(a+1),*(p-1));
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment