473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1320
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bob Hairgrove | last post by:
The following class contains start and end points of a range of values. The range can have a direction which is implied from the relative order of start and end, or it can be without direction. IOW...
3
by: kohvirus | last post by:
Well after embaressing myself and posting in the wrong fourm, I found my way to the right one and I'm hoping to seek some help. This is the program without any modifications that I am making...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
5
by: JS | last post by:
I give a function a void pointer as an argument. But in the function I would like to treat this argument as an integer (only pointers to integers will be sent to the function) therefor I would like...
4
by: pcnate | last post by:
I've been having some problems with pointers and such.This is homework, so I don't want people writing codeand telling me to use it. I just want some direction on what isn't working. here is...
9
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the...
9
by: olf | last post by:
Hello all, I have very recently trying to make a small program in c++ and I am having problems with pointers. I want to read a line from a file, send that line to a function that parses and...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
84
by: jacob navia | last post by:
As many people know, I think that garbage collection is a good solution for many memory allocation problems. I am aware however, that nothing is "the silver bullet", not even the GC. A recent...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.