469,575 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Problems with pointers

In the below code I try to initialize the first 5 elements in an array
to 27:
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);

int y;
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?
Jan 6 '07 #1
11 1153
Johs wrote:
In the below code I try to initialize the first 5 elements in an array
to 27:
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);
[...]
What am I missing?
I guess it should read:
(*p)[i] = 27;

Jan 6 '07 #2

"Johs" <sd*@sdf.comwrote in message news:en**********@news.net.uni-c.dk...
In the below code I try to initialize the first 5 elements in an array to
27:

#include <stdio.h>
typedef int list[10];

void bob(list * p)
void bob(int * p);
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);
bob(pop);
>
int y;
This is only valid for C99. If you want to conform with C89,
move this declaration to the beginning of 'main()', before the
call to 'bob()'.
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);
}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?
Apparently a better understanding of the relationship between
arrays and pointers in C.

-Mike
Jan 6 '07 #3

"Pierre L." <lo************@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Johs wrote:
>In the below code I try to initialize the first 5 elements in an array
to 27:
>typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);
[...]
>What am I missing?

I guess it should read:
(*p)[i] = 27;
No.

-Mike
Jan 6 '07 #4
Johs wrote:
In the below code I try to initialize the first 5 elements in an array
to 27:
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);

int y;
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?
#include <stdio.hand a compiler diagnostic.

When I try to compile your code I get the following error:
"10: error: incompatible types in assignment"

You are passing a pointer to the array to function bob. p[0] is the
first array of 10 ints, not the first int in your array. Since you
only have one array you are invoking undefined behavior when you try to
assign to p[1] which would try to assign the next (nonexistent) array
of 10 ints. Your compiler seems to be interpreting:
p[i] = 27; as *p[i] = 27;
which is wrong, the code should have produced a diagnostic message.

You can do at least two things to fix the code, change:
void bob(int * p)
and the call to bob with:
bob(pop);
This will cause a pointer to the first element of your array to be
passed to bob which will then be used to iterate over the int values in
the array.

of change the assignment to:
(*p)[i] = 27;
which will dereference p (yielding a pointer to the first element in
the array instead of a pointer to the array itself) before the
subscript operator is applied.

Check out section 6 of the FAQ (http://c-faq.com/), for insight on
arrays and pointers.

Robert Gamble

Jan 6 '07 #5
Johs said:
In the below code I try to initialize the first 5 elements in an array
to 27:
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);

int y;
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?
You're missing a <stdio.hinclusion, and you need to lose that ampersand in
the call to bob(). It should just be: bob(pop);
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 6 '07 #6
Richard Heathfield wrote:
Johs said:
In the below code I try to initialize the first 5 elements in an array
to 27:
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);

int y;
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?

You're missing a <stdio.hinclusion, and you need to lose that ampersand in
the call to bob(). It should just be: bob(pop);
If he does that he also needs to change the definition of bob to accept
a pointer to int.

Robert Gamble

Jan 6 '07 #7
Robert Gamble said:
Richard Heathfield wrote:
>Johs said:
What am I missing?

You're missing a <stdio.hinclusion, and you need to lose that ampersand
in the call to bob(). It should just be: bob(pop);

If he does that he also needs to change the definition of bob to accept
a pointer to int.
He had a pointer to list, didn't he? He did, didn't he? Sheesh, he had a
pointer to list.

Thanks, Robert.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 6 '07 #8
On Sat, 06 Jan 2007 17:09:38 +0100, Johs <sd*@sdf.comwrote:
>In the below code I try to initialize the first 5 elements in an array
to 27:
Your code has serious errors. Why did you ignore the diagnostics your
compiler is required to produce? If you didn't see the diagnostics,
you need to adjust your compiler options until you do.
>

typedef int list[10];
Your misunderstanding of how this typedef works is the root cause of
your problem. list is now a synonym for the type array of 10 int.
>
void bob(list * p)
p is not an array of 10 int. It is a pointer to an array of 10 int.
In C notation, it is int(*)[10]. Based on the code in this function,
you want to remove the asterisk.
>{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
p[i] is not the i-th element of pop from main. Since p is a pointer
to an array, p[i] is actually the i-th array p points to. It is an
array. Arrays cannot appear on the left of the assignment operator in
an assignment statement. Who knows what kind of code you compiler
generated for this?

When you remove the asterisk from the parameter list, p will be an
array and p[i] will be the i-th element of the array. This element
will be an int and you can then assign the value to that int.
> }
}

int main()
{

list pop;
pop is an array of 10 int.
bob(&pop);
The argument of this function call has type pointer to array of 10
int, which does match the parameter of the function as you coded it.

Once you fix the definition of bob, you must remove the ampersand to
match. This statement will then "pass the array" to the function.
When an array are passed to a function, the argument is automatically
converted to the address of the first element with type pointer to
element type. This is the exact equivalent of passing &pop[0] and is
precisely what the modified bob will be expecting.
>
int y;
C89 does not allow declarations to follow statements. Put all your
declarations at the top of the block.
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}

But it only prints:
pop[0] = 27
pop[1] = 0
pop[2] = 0
pop[3] = 0
pop[4] = 0

What am I missing?
The fact that your code is not valid C.
Remove del for email
Jan 6 '07 #9
Johs <sd*@sdf.comwrites:
[...]
typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);

int y;
for (y = 0; y < 5; y++)
{
printf("pop[%d] = %d\n",y,pop[y]);

}

return 0;

}
[...]

Did that code actually compile? Any conforming compiler is required
to produce a diagnostic for the code you've shown us; it's allowed to
go ahead and produce an executable after that, but the error is
serious enough that I'd be surprised if any compiler actually did so.
p[i] is of type list, i.e., it's an array of 10 ints; you can't assign
to an array.

I suspect this isn't the *exact* code that you compiled to produce the
output you showed us. I imagine that you might get that output from
something similar to the code you posted, but without seeing the
actual code it's hard to tell. Did you copy-and-paste the actual
code, or did you re-type it and perhaps introduce some additional
errors?

In any case, you're likely to find section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com>, illuminating.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 6 '07 #10
On Sat, 06 Jan 2007 17:14:13 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote:
>
"Pierre L." <lo************@gmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.com...
>Johs wrote:
>>In the below code I try to initialize the first 5 elements in an array
to 27:
>>typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);
[...]
>>What am I missing?

I guess it should read:
(*p)[i] = 27;

No.
Why not? It solves the syntax error and produces the desired result.
Remove del for email
Jan 7 '07 #11

"Barry Schwarz" <sc******@doezl.netwrote in message
news:vd********************************@4ax.com...
On Sat, 06 Jan 2007 17:14:13 GMT, "Mike Wahler"
<mk******@mkwahler.netwrote:
>>
"Pierre L." <lo************@gmail.comwrote in message
news:11**********************@s34g2000cwa.google groups.com...
>>Johs wrote:
In the below code I try to initialize the first 5 elements in an array
to 27:

typedef int list[10];

void bob(list * p)
{
int i;
for (i = 0; i < 5; i++)
{
p[i] = 27;
}
}

int main()
{

list pop;
bob(&pop);
[...]

What am I missing?

I guess it should read:
(*p)[i] = 27;

No.

Why not? It solves the syntax error and produces the desired result.
Yes, you (and Pierre)are right of course. I guess I didn't look at
it closely enough.

-Mike
Jan 7 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Bob Hairgrove | last post: by
3 posts views Thread by kohvirus | last post: by
5 posts views Thread by Sona | last post: by
5 posts views Thread by JS | last post: by
4 posts views Thread by pcnate | last post: by
9 posts views Thread by bwaichu | last post: by
9 posts views Thread by olf | last post: by
84 posts views Thread by jacob navia | last post: by
14 posts views Thread by stevenruiz | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.