IAMTubby
October 16th, 2014, 03:01 AM
I was trying to populate an array using a pointer and it's given me a few results which I'm finding difficult to comprehend. I'm sorry for the long post, and I have tried to figure out the possible reasons, but every time I try something, it throws up something new.(Sorry also, for the smiley in the question, I didn't mean to put it there and am not able to edit it now, so let it be :) )
The array name 'l' used in the questions below signifies it's a local pointer - l for local.
1. This code works in one compiler, doesn't work in another
I understand that it's basically because on the one where it works, the address of l is not null, and yes null in the other one.
My question basically is, how do I access an array using pointers/
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l;
int i =0;
/* I did try printing out the address here and check on both machines - As expected, it gives null on the machine where the code gives a seg fault, and not null in the machine where it works */
printf("l == [%p]\n",l);
for(i=0;i<10;i++)
*(l+i) = i+1;
for(i=0;i<10;i++)
printf("%d ",l[i]);
return 0;
}
2.Now consider this code where I have only added one more pointer in the exact same place where I had used l in the previous example. This gives me a seg fault even in the machine where the previous code was working. Wow!
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l1;
int* l2;
int i =0;
/* I did try printing out the address here and check on both machines */
printf("l1 == [%p]\n",l1);
printf("l2 == [%p]\n",l2);
for(i=0;i<10;i++)
{
*(l1+i) = i+1;
*(l2+i) = i+1;
}
for(i=0;i<10;i++)
{
printf("%d ",l1[i]);
printf("%d ",l2[i]);
}
return 0;
}
This is the output I get
l1 == [0x400410]l2 == [0x7ffffb090b90]
Segmentation fault (core dumped)
3.And now, what's the default address a pointer points to ? I think it is :
Default value of local pointer = random address,
Default value of global pointer = NULL,
Am I correct?
4.Realizing that code in 1 does not work for one of the compilers because it takes the value as NULL, I tried to malloc addresses before putting in values. Please can you tell me how it's done. I tried doing it(see code below), but this it gives me : error: lvalue required as left operand of assignment
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l1;
int i =0;
/* I did try printing out the address here and check on both machines */
printf("l1 == [%p]\n",l1);
for(i=0;i<10;i++)
{
(l1 + i) = malloc(200 * sizeof(char));
*(l1+i) = i+1;
}
for(i=0;i<10;i++)
{
printf("%d ",l1[i]);
}
return 0;
}
Please advise.
And once again, sorry for the long post.
The array name 'l' used in the questions below signifies it's a local pointer - l for local.
1. This code works in one compiler, doesn't work in another
I understand that it's basically because on the one where it works, the address of l is not null, and yes null in the other one.
My question basically is, how do I access an array using pointers/
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l;
int i =0;
/* I did try printing out the address here and check on both machines - As expected, it gives null on the machine where the code gives a seg fault, and not null in the machine where it works */
printf("l == [%p]\n",l);
for(i=0;i<10;i++)
*(l+i) = i+1;
for(i=0;i<10;i++)
printf("%d ",l[i]);
return 0;
}
2.Now consider this code where I have only added one more pointer in the exact same place where I had used l in the previous example. This gives me a seg fault even in the machine where the previous code was working. Wow!
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l1;
int* l2;
int i =0;
/* I did try printing out the address here and check on both machines */
printf("l1 == [%p]\n",l1);
printf("l2 == [%p]\n",l2);
for(i=0;i<10;i++)
{
*(l1+i) = i+1;
*(l2+i) = i+1;
}
for(i=0;i<10;i++)
{
printf("%d ",l1[i]);
printf("%d ",l2[i]);
}
return 0;
}
This is the output I get
l1 == [0x400410]l2 == [0x7ffffb090b90]
Segmentation fault (core dumped)
3.And now, what's the default address a pointer points to ? I think it is :
Default value of local pointer = random address,
Default value of global pointer = NULL,
Am I correct?
4.Realizing that code in 1 does not work for one of the compilers because it takes the value as NULL, I tried to malloc addresses before putting in values. Please can you tell me how it's done. I tried doing it(see code below), but this it gives me : error: lvalue required as left operand of assignment
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int* l1;
int i =0;
/* I did try printing out the address here and check on both machines */
printf("l1 == [%p]\n",l1);
for(i=0;i<10;i++)
{
(l1 + i) = malloc(200 * sizeof(char));
*(l1+i) = i+1;
}
for(i=0;i<10;i++)
{
printf("%d ",l1[i]);
}
return 0;
}
Please advise.
And once again, sorry for the long post.