Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What's wrong with this code? (http://www.velocityreviews.com/forums/t658885-whats-wrong-with-this-code.html)

LL 01-11-2009 08:57 AM

What's wrong with this code?
 
#include <stdio.h>
#include <stdlib.h>

struct Student {
int id;
char* name;
};


void getStudents(struct Student*, int);
void printStudents(struct Student*, int);

main() {
int n;
struct Student *s;
printf("Number of students: ");
scanf("%d", &n);
s=(struct Student*)malloc(n*(sizeof(struct Student)));
getStudents(s,n);
printStudents(s,n);
free(s);
exit(0);
}

void getStudents(struct Student *s, int n) {
for (int i=0; i<n; i++) {
printf("Student Name: ");
scanf("%s", s->name);
printf("Student ID: ");
scanf("%d", &s->id);
s++;
}
}


void printStudents(struct Student *s, int n) {
for(int i=0;i<n;i++) {
printf("Student Name: %s", s->name);
printf("ID: %d", s->id);
s++;
}
}

Ian Collins 01-11-2009 09:08 AM

Re: What's wrong with this code?
 
LL wrote:
> #include <stdio.h>
> #include <stdlib.h>
>

Are you attempting to write C or C++?

> struct Student {
> int id;
> char* name;
> };
>
>
> void getStudents(struct Student*, int);
> void printStudents(struct Student*, int);
>
> main() {
> int n;
> struct Student *s;
> printf("Number of students: ");
> scanf("%d", &n);
> s=(struct Student*)malloc(n*(sizeof(struct Student)));
> getStudents(s,n);
> printStudents(s,n);
> free(s);
> exit(0);
> }
>
> void getStudents(struct Student *s, int n) {
> for (int i=0; i<n; i++) {
> printf("Student Name: ");
> scanf("%s", s->name);
> printf("Student ID: ");
> scanf("%d", &s->id);
> s++;
> }
> }
>
>
> void printStudents(struct Student *s, int n) {
> for(int i=0;i<n;i++) {
> printf("Student Name: %s", s->name);
> printf("ID: %d", s->id);
> s++;
> }
> }


What's your question? What do you think is wrong?

--
Ian Collins

LL 01-11-2009 09:20 AM

Re: What's wrong with this code?
 
On Sun, 11 Jan 2009 20:08:43 +1100, Ian Collins <ian-news@hotmail.com>
wrote:

> LL wrote:
>> #include <stdio.h>
>> #include <stdlib.h>
>>

> Are you attempting to write C or C++?
>
>> struct Student {
>> int id;
>> char* name;
>> };
>>
>>
>> void getStudents(struct Student*, int);
>> void printStudents(struct Student*, int);
>>
>> main() {
>> int n;
>> struct Student *s;
>> printf("Number of students: ");
>> scanf("%d", &n);
>> s=(struct Student*)malloc(n*(sizeof(struct Student)));
>> getStudents(s,n);
>> printStudents(s,n);
>> free(s);
>> exit(0);
>> }
>>
>> void getStudents(struct Student *s, int n) {
>> for (int i=0; i<n; i++) {
>> printf("Student Name: ");
>> scanf("%s", s->name);
>> printf("Student ID: ");
>> scanf("%d", &s->id);
>> s++;
>> }
>> }
>>
>>
>> void printStudents(struct Student *s, int n) {
>> for(int i=0;i<n;i++) {
>> printf("Student Name: %s", s->name);
>> printf("ID: %d", s->id);
>> s++;
>> }
>> }

>
> What's your question? What do you think is wrong?
>

When I compile and run the program it crashes.


news 01-11-2009 09:26 AM

Re: What's wrong with this code?
 
#define maxLen 100
struct Student {
int id;
char name[maxLen];
};
> struct Student {
> int id;
> char* name;
> };
> scanf("%s", s->name);



LL 01-11-2009 09:31 AM

Re: What's wrong with this code?
 
On Sun, 11 Jan 2009 20:26:39 +1100, news <news@cn99.com> wrote:

> #define maxLen 100
> struct Student {
> int id;
> char name[maxLen];
> };
>> struct Student {
>> int id;
>> char* name;
>> };
>> scanf("%s", s->name);

>


That fixes it.

Rolf Magnus 01-11-2009 09:34 AM

Re: What's wrong with this code?
 
LL wrote:

> #include <stdio.h>
> #include <stdlib.h>
>
> struct Student {
> int id;
> char* name;


You really should use strings (std::string) instead of raw character arrays.

> };
>
>
> void getStudents(struct Student*, int);
> void printStudents(struct Student*, int);
>
> main() {


Your main() function is missing a return type.

> int n;
> struct Student *s;
> printf("Number of students: ");
> scanf("%d", &n);
> s=(struct Student*)malloc(n*(sizeof(struct Student)));


Prefer operator new over malloc in C++:

s = new Student[n];

Or even better, use std::vector and don't manually allocate dynamic memory
at all. Especially if you're a beginner, that will make things a lot easier.

> getStudents(s,n);
> printStudents(s,n);
> free(s);


delete [] s; // if you use operator new

> exit(0);
> }
>
> void getStudents(struct Student *s, int n) {
> for (int i=0; i<n; i++) {
> printf("Student Name: ");
> scanf("%s", s->name);


s->name is an uninitialized pointer. So you're writing to whatever random
memory location it currently happens to point to. You need to let it point
to some memory that can take the character data to be read from stdin.
Unfortunately, you don't know how many characters the user is going to
enter. What you can do is allocate some large enough block of memory before
calling scanf and then limit the number of characters that it reads. This
can be done with the format string. Much simpler, and without that limit,
would be to use std::string and std::cin.

> printf("Student ID: ");
> scanf("%d", &s->id);
> s++;
> }
> }
>
>
> void printStudents(struct Student *s, int n) {
> for(int i=0;i<n;i++) {
> printf("Student Name: %s", s->name);
> printf("ID: %d", s->id);
> s++;
> }
> }



Rolf Magnus 01-11-2009 09:52 AM

Re: What's wrong with this code?
 
LL wrote:

> On Sun, 11 Jan 2009 20:26:39 +1100, news <news@cn99.com> wrote:
>
>> #define maxLen 100
>> struct Student {
>> int id;
>> char name[maxLen];
>> };
>>> struct Student {
>>> int id;
>>> char* name;
>>> };
>>> scanf("%s", s->name);

>>

>
> That fixes it.


Unless the user types a name that is longer than 99 characters, in which
case you get a buffer overflow, one of the most-exploited types of security
leaks in real-world software.


Rui Maciel 01-11-2009 11:29 AM

Re: What's wrong with this code?
 
LL wrote:

> #include <stdio.h>
> #include <stdlib.h>
>
> struct Student {
> int id;
> char* name;
> };
>
>
> void getStudents(struct Student*, int);
> void printStudents(struct Student*, int);
>
> main() {
> int n;
> struct Student *s;
> printf("Number of students: ");
> scanf("%d", &n);
> s=(struct Student*)malloc(n*(sizeof(struct Student)));
> getStudents(s,n);
> printStudents(s,n);
> free(s);
> exit(0);
> }


<snip />


You defined struct Student and in the main() function you allocate memory for instances of struct Student. Yet, notice that you never allocate memory for char *name, which means that in the getStudent() and printStudent() procedures, when you try to access the name fields, you are attempting to read from memory that, as you never allocated, isn't yours. That's a no no.

Other than that, it appears that you only rely on C language constructs and libraries, not C++. Are you trying to learn C or C++?


Hope this helps,
Rui Maciel

Leson 01-12-2009 01:52 AM

Re: What's wrong with this code?
 
Firstly, you do not have to name the members of the structure allocate
memory space to store the string;

Secondly, in your GetStudents function the pointer of the structure should
be an alternative variable, otherwise you will not print the correct
structure, because the structure pointer displacement has occurred, and not
the original location. Similarly printStudents.

"LL" <10464307@uts.edu.au> ??????:op.unk4ibgccqb7ql@user1-pc...
> #include <stdio.h>
> #include <stdlib.h>
>
> struct Student {
> int id;
> char* name;
> };
>
>
> void getStudents(struct Student*, int);
> void printStudents(struct Student*, int);
>
> main() {
> int n;
> struct Student *s;
> printf("Number of students: ");
> scanf("%d", &n);
> s=(struct Student*)malloc(n*(sizeof(struct Student)));
> getStudents(s,n);
> printStudents(s,n);
> free(s);
> exit(0);
> }
>
> void getStudents(struct Student *s, int n) {
> for (int i=0; i<n; i++) {
> printf("Student Name: ");
> scanf("%s", s->name);
> printf("Student ID: ");
> scanf("%d", &s->id);
> s++;
> }
> }
>
>
> void printStudents(struct Student *s, int n) {
> for(int i=0;i<n;i++) {
> printf("Student Name: %s", s->name);
> printf("ID: %d", s->id);
> s++;
> }
> }





All times are GMT. The time now is 04:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.