473,385 Members | 1,888 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.

Need helps on an array of pointers to structure

2
I have problems to access a member in an array of pointers to structure.
I declare the following structure

struct {
char name[80];
float mark;
}*rec[10];

I would like to put data to rec[0]. So I use following codes

gets(rec[0]->name);
scanf("%f", rec[0]->mark);

the gets command is working fine, But the command for rec[0]->mark generate an error saying that the floting point doesn't linked.

Can any one help me?

More.... it is working fine if I declare an extra floating var, eg. float a and do the follwoing
scanf("%f", &a);
rec[0]->mark = a;
Oct 11 '06 #1
7 3236
koder
23
i got a differnt warnig from GCC

the `gets' function is dangerous and should not be used.
why is it so?
Oct 11 '06 #2
Try using this :

scanf("%f", &rec[0]->mark);
Oct 11 '06 #3
I am not sure why but I tried this piece of code and it worked fine for me :
----------------------------------------------------------------------
#include <iostream.h>
#include <stdio.h>

struct Rec {
char name[80];
float mark;
};



void main ()
{

printf("Entering prog...\n");
Rec * rec[10];

/*gets(rec[0]->name);*/
scanf("%s", &rec[0]->name);
printf("After reading name...\n");
scanf("%f", &rec[0]->mark);

printf("Name - <%s>: Marks - <%f>", rec[0]->name, rec[0]->mark);

}


------------------------------------------------------------------------------------

I am not sure if this explanation sounds reasonable to you or not but I think it happened because when we said :

scanf("%f", rec[0]->mark);

The rec[0]->mark still didnt return the reference to the variable "mark" whereas scanf expects a reference to the variable's memory location as in :

scanf("%f", &a);
Oct 11 '06 #4
Ying
2
It tried what you suggested, but it doesn't work.
It still said the floating point doesn't linked for

scanf("%f", &rec[0]->mark);

but it works fine for

scanf("%s", &rec[0]->name);

This is so strange. Huh!


I am not sure why but I tried this piece of code and it worked fine for me :
----------------------------------------------------------------------
#include <iostream.h>
#include <stdio.h>

struct Rec {
char name[80];
float mark;
};



void main ()
{

printf("Entering prog...\n");
Rec * rec[10];

/*gets(rec[0]->name);*/
scanf("%s", &rec[0]->name);
printf("After reading name...\n");
scanf("%f", &rec[0]->mark);

printf("Name - <%s>: Marks - <%f>", rec[0]->name, rec[0]->mark);

}


------------------------------------------------------------------------------------

I am not sure if this explanation sounds reasonable to you or not but I think it happened because when we said :

scanf("%f", rec[0]->mark);

The rec[0]->mark still didnt return the reference to the variable "mark" whereas scanf expects a reference to the variable's memory location as in :

scanf("%f", &a);
Oct 11 '06 #5
Banfa
9,065 Expert Mod 8TB
i got a differnt warnig from GCC


why is it so?
The reason gets is dangerous and it is recomend that you dont use it (and the same goes for scanf too if it is used to enter a string) is that you provide a pointer to an array of characters but you do not tell the function how long the array is. This means that the user can enter data that is longer than the array of characters and force an overrun error where gets overwrites the variables following the array. This is a technique used by hackers to break into systems.

the safe version of gets is fgets

char * fgets (char * string , int num , FILE * stream);

This is safe because you pass the size of the array you have passed a pointer to and the function ensures that only that amount of data is written no matter how much data the user inputs preventing over run errors. Although this takes a FILE * for the input stream you can still use this function by using the system defined stream stdin

Expand|Select|Wrap|Line Numbers
  1. char text[20];
  2.  
  3. gets(text);  // <-- bad user may enter more than 19 characters
  4.  
  5. fgets(text, sizeof text, stdin);  // <-- good array text is proctected 
  6.                                   // from over run errors
  7.  
Oct 11 '06 #6
Banfa
9,065 Expert Mod 8TB
It tried what you suggested, but it doesn't work.
It still said the floating point doesn't linked for

scanf("%f", &rec[0]->mark);

but it works fine for

scanf("%s", &rec[0]->name);

This is so strange. Huh!
This is more than strange it is wrong. Here are some of your errors

1. You declare Rec * rec[10]; then you start accesing the members pointed to by rec[0] without allocating any memory to that pointer. As soon as you do this you invoke undefined behaviour. Once undefined behaviour has been invoked anywhere in your code all bets are off. The code may work as expected or it may crash or it may perform some completely unexpected behaviour. Don't do it.

2. You have to pass pointers to basic types to scanf to the locations to write data back to so

scanf("%f", rec[0]->mark);

rec[0]->mark is not a pointer it is a float, this wont work

scanf("%s", &rec[0]->name);

rec[0]->name is already a pointer, &rec[0]->name is a pointer to a pointer to char (char **), scanf does not understand this, it wont work

These are the working version of these lines


scanf("%f", &rec[0]->mark);
scanf("%s", rec[0]->name);

3. I very much doubt it said "floating point doesn't linked", most compilers manage to use resonable English which this isn't. If you are getting an error post the actual error not your interpretation of it.
Oct 11 '06 #7
This is more than strange it is wrong. Here are some of your errors

1. You declare Rec * rec[10]; then you start accesing the members pointed to by rec[0] without allocating any memory to that pointer. As soon as you do this you invoke undefined behaviour. Once undefined behaviour has been invoked anywhere in your code all bets are off. The code may work as expected or it may crash or it may perform some completely unexpected behaviour. Don't do it.

2. You have to pass pointers to basic types to scanf to the locations to write data back to so

scanf("%f", rec[0]->mark);

rec[0]->mark is not a pointer it is a float, this wont work

scanf("%s", &rec[0]->name);

rec[0]->name is already a pointer, &rec[0]->name is a pointer to a pointer to char (char **), scanf does not understand this, it wont work

These are the working version of these lines


scanf("%f", &rec[0]->mark);
scanf("%s", rec[0]->name);

3. I very much doubt it said "floating point doesn't linked", most compilers manage to use resonable English which this isn't. If you are getting an error post the actual error not your interpretation of it.


Hi ,
include <float.h> this will solve your problem
and make it sure that you are allocating the memory
for each individual pointers
If not so then your application may crash or core dump.
Oct 11 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
8
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
12
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...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
2
by: sunilvalli | last post by:
Hi All, I would like know how to allocate memory for an array of structure pointers..Like for example typedef struct List { char* Name; char* Address; BOOL is_present
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.