473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to struct

can´t understand why the program above not works.
thanks all
#include <stdio.h>
#include <stdlib.h>

#define SIZE 3

struct peoples
{
int code;
char name[20];
float value;
};

int main()
{
int a;
struct peoples *people;
people=(struct peoples *)malloc(SIZE*s izeof(struct peoples));
for(a=0;a<SIZE; a++)
{
people->code=a;
scanf("%s",peop le->name);
scanf("%f",peop le->value);
people++;
}
for(a=0;a<SIZE; a++)
{
printf("%d",peo ple->code);
printf("%s",peo ple->name);
printf("%f",peo ple->value);
people++;
}
}

Dec 6 '06
14 2531
povoação wrote:
It´s true. I saved .c and now compiles. I´m using Dev-C++ now.
Can help me to fix the program to work with pointers notation?

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Dec 6 '06 #11
On Dec 6, 4:10 pm, "matevzb" <mate...@gmail. comwrote:
povoação wrote:
can´t understand why the program above not works.For many reasons.
people=(struct peoples *)malloc(SIZE*s izeof(struct peoples));
for(a=0;a<SIZE; a++)
{
people->code=a;
scanf("%s",peop le->name);
scanf("%f",peop le->value);
people++;
}You're incrementing people (pointer to struct peoples), which after
the
first for loop points to where? You'd be better of with:
people[a].code = a;
Secondly, scanf() arguments should be pointers so use:
scanf ("%f", &people[a].value);
For char arrays, you can use either people[a].name or
&people[a].name[0]. Some prefer the second version.
Thirdly, you're not checking for scanf() return values. If someone
doesn't enter a float when you expect it, your program will misbehave.
Also, your people[a].name is limited to 20 characters, it won't work if
someone enters more than that - check the scanf() man page.
}
for(a=0;a<SIZE; a++)
{
printf("%d",peo ple->code);
printf("%s",peo ple->name);
printf("%f",peo ple->value);
people++;
}Again, don't increment people, use people[a]. Outputting a newline
now
and then may also be a good thing.
"povoação" <ok*****@gmail. comwrote in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
I modified to people[a] instead pointer and works well, but I´m
learning pointers and I can´t understand cause was not working with
pointer.
in your first loop, you included:
for(a=0;a<SIZE; a++) {

people++;
}

So, where does "people" now point?

Then your second loop:

for(a=0;a<SIZE; a++) {
printf("%d",peo ple->code);
people++;
}

Just because you began a new loop, this does not
mean the "people" magically again points to the
original place.

If you insist on using pointers, try:

struct peoples *p = people;
for(a=0;a<SIZE; a++) {
scanf("%s",p->name);
scanf("%f",&p->value);
p++;
}
p = people; /* resets to point to origin of allcated structs */
for(a=0;a<SIZE; a++) {
...
p++;
}

(still contains problems pointed out by others)

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Dec 6 '06 #12
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
"povoação" <ok*****@gmail. comwrites:
>Can help me to fix the program to work with pointers notation?

If a problem is easily solved using array notation, then there is
no need to solve it another way.
Unless the actual problem is learning to use pointer notation.

--
Keith Thompson (The_Other_Keit h) 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.
Dec 6 '06 #13
Ben Pfaff wrote:
"ls@qustation.c om" <ls******@gmail .comwrites:
You only have on struct and one pointer.

Read his code again. He allocates an array of SIZE structs.
And then uses it as if it held 2 * SIZE structs. Boom.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 7 '06 #14
thanks all. Now works with pointers. Thanks for the tips too.

On Dec 6, 11:03 pm, CBFalconer <cbfalco...@yah oo.comwrote:
Ben Pfaff wrote:
"l...@qustation .com" <lsear...@gmail .comwrites:
You only have on struct and one pointer.
Read his code again. He allocates an array of SIZE structs.And then uses it as if it held 2 * SIZE structs. Boom.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 7 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7654
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
4115
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
5
2530
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is going to hell just by using dlopen()/LoadLibrary() respectively, I'm still trying to get it as clean as possible. I have a number of different quantums of data and a number of plugins. Since any plugin can (and possibly will) touch any quantum...
2
3055
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
4
3602
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
10
353
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without change. My question is that, is it legal to dereference the intermediate pointer (the pointer with less strict alignment that we get after conversion) ? Does this intermediate pointer also point to the same memory location as the pointer (with more...
5
4341
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read that there is no difference between giving this function a
2
1770
by: Chad | last post by:
The following question stems from the following thread on comp.lang.c: http://groups.google.com/group/comp.lang.c/browse_thread/thread/0ad03c96df57381a/5f20260b30952fe7?hl=en#5f20260b30952fe7 I was sort of mystified by a comment made by Eric. And I quote: "You might also be confused by the fact that each `struct my_struct' contains a pointer to a `struct my_struct' object. There is nothing special about that inner pointer:
12
3878
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
2
3272
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.