simple C question

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have looked all over the internet for a tutorial on how to do this
because I've forgot because I usually manually load arrays. But in such a
case where say someone has an array with 100 elements I want to fill each
with the numbers 0-99.
I say 2 fors are going to be needed and 2 arrays. Then I can point an
int somewhere to get the value of an element. This is probably simpler than
I'm imagining. I need a refresher.

Bill
 
P

Prathamesh Kulkarni

int main(void)
{
int a[100], i;

for (i = 0; i < 100; i++)
a = i;
return 0;
}
 
B

Bill Cunningham

Prathamesh said:
int main(void)
{
int a[100], i;

for (i = 0; i < 100; i++)
a = i;
return 0;
}


lol <chuckle> I see that's right. No tutorial I saw said this. It was
i=a; or just iterating over an array without doing anything. lvaules.
Thanks much.

Bill
 
B

Bill Cunningham

Prathamesh said:
Prathamesh said:
int main(void)

int a[100], i;
for (i = 0; i < 100; i++)

return 0;



lol <chuckle> I see that's right. No tutorial I saw said this. It was

i=a; or just iterating over an array without doing anything.
lvaules.

Thanks much.



Bill


you could try i[a] = i as well ;)


This code works but it prints a seg fault right after the number 54.

#include <stdio.h>
#include <string.h>

int main(void)
{
int a[100] = { '\0' };
int i = 1;
for (; i < sizeof a; i++)
a = i;
printf("%d\n", a[54]);
return 0;
}

And it does it whether I set the entire array to 0 or not. Any guess what's
up? And I compiled with -Wall and -ansi too.

Bill
 
B

Bill Cunningham

Bill said:
Prathamesh said:
Prathamesh Kulkarni wrote:

int main(void)

{

int a[100], i;



for (i = 0; i < 100; i++)

a = i;

return 0;

}



lol <chuckle> I see that's right. No tutorial I saw said this. It
was i=a; or just iterating over an array without doing anything.
lvaules.

Thanks much.



Bill


you could try i[a] = i as well ;)


This code works but it prints a seg fault right after the number
54.
#include <stdio.h>
#include <string.h>

int main(void)
{
int a[100] = { '\0' };
int i = 1;
for (; i < sizeof a; i++)
a = i;
printf("%d\n", a[54]);
return 0;
}

And it does it whether I set the entire array to 0 or not. Any guess
what's up? And I compiled with -Wall and -ansi too.

Bill


Ok is it the string.h not being used? I forgot to take it out after removing
memset to zero out the array and using {'\0'}

Bill
 
B

Bill Cunningham

Ian said:
This code works but it prints a seg fault right after the
number 54.

Now I'm sure you really are taking the piss.
#include<stdio.h>
#include<string.h>
Why?

int main(void)
{
int a[100] = { '\0' };

To zero out the whole array.
Why '\0'?

I guess with ints it means the same as zero.

I want to index to 1.
What is sizeof a?

An array called a of 100 elements. I guess I could've used.

sizeof (int)*100;

Bill
 
I

Ian Collins

Ian said:
This code works but it prints a seg fault right after the
number 54.

Now I'm sure you really are taking the piss.
#include<stdio.h>
#include<string.h>
Why?

int main(void)
{
int a[100] = { '\0' };

To zero out the whole array.
Why '\0'?

I guess with ints it means the same as zero.

Then why not use 0?
I want to index to 1.

To 1?
An array called a of 100 elements. I guess I could've used.

sizeof (int)*100;

Any what is the value of that expression?
 
P

Prathamesh Kulkarni

Ian said:
On 11/05/12 11:39, Bill Cunningham wrote:
This code works but it prints a seg fault right after the
number 54.
Now I'm sure you really are taking the piss.
#include<stdio.h>
#include<string.h>
int main(void)
{
int a[100] = { '\0' };



To zero out the whole array.


Why '\0'?



I guess with ints it means the same as zero.





I want to index to 1.


What is sizeof a?



An array called a of 100 elements. I guess I could've used.



sizeof (int)*100;



Bill

why do you want to initalize the array with zeroes when you are filling it in the loop ?

you are segfaulting because of the loop. you want to loop till i < 100 and the for loop terminates when i become sizeof(int) * 100
 
B

Bill Cunningham

Ian said:
Ian said:
On 11/05/12 11:39, Bill Cunningham wrote:

This code works but it prints a seg fault right after the
number 54.

Now I'm sure you really are taking the piss.

#include<stdio.h>
#include<string.h>

Why?

int main(void)
{
int a[100] = { '\0' };

To zero out the whole array.
Why '\0'?

I guess with ints it means the same as zero.

Then why not use 0?

To get used to using the null character for when working with chars.

instead of 0 to 99 1 to 100.
Any what is the value of that expression?

Huh. Well I guess 100 instances of however the sizeof ints are on my
machine. I'm running a 64 bit OS.
 
I

Ian Collins

If you are going to use the awful google Usenet interface, please clean
up all the extra blank lines it adds and wrap your lines to something
sensible. Thanks.
 
B

Bill Cunningham

Prathamesh said:
why do you want to initalize the array with zeroes when you are
filling it in the loop ?

As habit I guess. It's better than manual initialization with 100
elements.
you are segfaulting because of the loop. you want to loop till i <
100 and the for loop terminates when i become sizeof(int) * 100

SO I want

for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?

Bill
 
I

Ian Collins

As habit I guess. It's better than manual initialization with 100
elements.


SO I want

for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?

Your head?
 
B

Bill Cunningham

Ian said:
Ian said:
On 11/05/12 11:50, Bill Cunningham wrote:
Ian Collins wrote:
On 11/05/12 11:39, Bill Cunningham wrote:

#include<stdio.h>
#include<string.h>

Why?

int main(void)
{
int a[100] = { '\0' };

To zero out the whole array.

Why '\0'?

I guess with ints it means the same as zero.

Then why not use 0?

To get used to using the null character for when working with
chars.

So do you practice dog training on your cat?
instead of 0 to 99 1 to 100.

Given your array declaration, what is the last valid entry in your
array?
Huh. Well I guess 100 instances of however the sizeof ints are on my
machine. I'm running a 64 bit OS.

So the value is?

Not sure. Probably 16 to 32 bytes.
Is that a valid index to your array?

I don't know for sure that one. Maybe this is not a case for sizeof.

Bill
 
S

SG

Am 04.11.2012 23:39, schrieb Bill Cunningham:
This code works but it prints a seg fault right after the number 54.

#include <stdio.h>
#include <string.h>

int main(void)
{
int a[100] = { '\0' };
int i = 1;
for (; i < sizeof a; i++)
a = i;
printf("%d\n", a[54]);
return 0;
}

And it does it whether I set the entire array to 0 or not. Any guess what's
up?


sizeof a is not the threshold you are looking for.
 
P

Prathamesh Kulkarni

If you are going to use the awful google Usenet interface, please clean

up all the extra blank lines it adds and wrap your lines to something

sensible. Thanks.

I apologize for that, I am a newbie in this group.
Would keep that in mind ;)
 
P

Prathamesh Kulkarni

Prathamesh Kulkarni wrote:







As habit I guess. It's better than manual initialization with 100

elements.

No, I mean when you are going to fill
the entire array in the loop, there's
no point with array initialization
SO I want



for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?



Bill

no, the '\0' does not mess up anything,
although 0 would be a better practice.

It's the loop, it should be written as:
for (i = 0; i < 100; i++)
a = i;
in the condition,
i < sizeof a, you would also be using indexes
that are out-of-bounds, hence the seg-fault
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top