Array problem

M

madhura

Hello,
I have a problem with an array, The code is:
main()
{
char s[]="Hello";
int i=0;
while(s!=0)
{
printf("\n%c %c",s,*(s+i));
printf("\n%c %c",i,*(i+s));
i++;
}
}
The output of above code is H H
H H
e e
e e
till o.
I have a problem with the second printf statement, I have not declared
i[] array, then how come that i am getting this output, how that
expression is interpreted.
Cheers
Madhura
 
P

pemo

madhura said:
Hello,
I have a problem with an array, The code is:
main()
{
char s[]="Hello";
int i=0;
while(s!=0)
{
printf("\n%c %c",s,*(s+i));
printf("\n%c %c",i,*(i+s));
i++;
}
}
The output of above code is H H
H H
e e
e e
till o.
I have a problem with the second printf statement, I have not
declared i[] array, then how come that i am getting this output, how
that expression is interpreted.
Cheers
Madhura


s by itself resolves to be the address of element 1, i.e., s == &s[0].

printf is expecting a character, and, whether you take s's address, and add
i to it, or take i, and add s's address to it, you'll end up at the same
place.

"Hello"[0] --- that's 'H'

0["Hello"] --- that's also 'H'.

Any clearer?
 
P

pemo

pemo said:
madhura said:
Hello,
I have a problem with an array, The code is:
main()
{
char s[]="Hello";
int i=0;
while(s!=0)
{
printf("\n%c %c",s,*(s+i));
printf("\n%c %c",i,*(i+s));
i++;
}
}
The output of above code is H H
H H
e e
e e
till o.
I have a problem with the second printf statement, I have not
declared i[] array, then how come that i am getting this output, how
that expression is interpreted.
Cheers
Madhura


s by itself resolves to be the address of element 1, i.e., s == &s[0].

printf is expecting a character, and, whether you take s's address,
and add i to it, or take i, and add s's address to it, you'll end up
at the same place.

"Hello"[0] --- that's 'H'

0["Hello"] --- that's also 'H'.

Any clearer?


Here's a tiny mod to your code that might help - and let this also serve as
the /normal/ 'you forgot this, that, the other' *slapping*.


#include <stdio.h>

int main(void)
{
char s[] = "Hello";

int i = 0;

do
{
printf("i = %d, s's address = %p\n\t\t", i, s);

printf("so, i + s = %p, and, of course, s + i = %p\n\t\t", i + s, s
+ i);

// for the %c - any of these is ok .. s, i, *(s + i), *(i + s)
// it's just adding numbers!
//
printf("and, the char at %p is %c - however you put i & s
together\n\n", i + s, s);

}while(s[++i] != 0);

getchar();

return 0;
}
 
V

Vladimir S. Oka

I have understood the *(i+s) but not i.


Quote context. Read:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

As for your confusion: if s is the same as *(s+i), and addition being
commutative operation, even on pointers (i.e. s+i gives the same result
as i+s), then *(s+i) is the same as *(i+s) is the same as i. Weird,
but possible in C. Also read the FAQ entry you were pointed to.

--
BR, Vladimir

Ehrman's Commentary:
(1) Things will get worse before they get better.
(2) Who said things would get better?
 
R

Robin Haigh

pemo said:
madhura said:
Hello,
I have a problem with an array, The code is:
main()
{
char s[]="Hello";
int i=0;
while(s!=0)
{
printf("\n%c %c",s,*(s+i));
printf("\n%c %c",i,*(i+s));
i++;
}
}
The output of above code is H H
H H
e e
e e
till o.
I have a problem with the second printf statement, I have not
declared i[] array, then how come that i am getting this output, how
that expression is interpreted.
Cheers
Madhura


s by itself resolves to be the address of element 1, i.e., s == &s[0].

printf is expecting a character, and, whether you take s's address, and add
i to it, or take i, and add s's address to it, you'll end up at the same
place.

"Hello"[0] --- that's 'H'

0["Hello"] --- that's also 'H'.



and of course 5["Hello"]["Hello"] is also 'H'.

Works with multi-dimensional arrays as well: k[j[i[arr]]]
 

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
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top