I have been programming with GCC for a while but I decided to start working with Visual Studio instead, because it is comfortable. Anyways, the problem is that in Visual studio, dynamic arrays do not really work like they do in GCC, so i used malloc instead.
When printing the 1st value assigned I will get a correct answer. However, the next values will be wrong, the 2nd value will always be wrong, the third value wrong and I can not figure out why.
For example, when inserting 1,2,3 and try to print the 2nd spot, it gives 5.
int main(void)
{
K1();
printf("%d\n", p1+1);
return 0;
]
With p1[i] the program crashes. p1, is int *
void K1(void)
{
int i;
printf("n1");
scanf_s("%d", &n1);
p1 = (int*)malloc(sizeof(int) * n1);
for(i=0;i<n1;i++)
{
scanf_s("%d", &p1 + i*sizeof(int));
}
}
I can not figure out what is the problem.