Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string tables 1

Status
Not open for further replies.

bigtamscot

Programmer
Apr 28, 2001
37
GB
Hi, I am studying C, and i am having problems with a project I have to complete and hand in for marking asap. The trouble is with multi-dimensional string tables. I have to create a list of student names and the course they are enrolled in at college. Here is a snippet of what I have so far:-

int void main( )
{
char student[MAX][30], char course[MAX][30];
char *s_ptr, *c_ptr;
int i;

s_ptr = student; c_ptr = course;
for(i = 0; i < MAX - 1;i++)
{
printf(&quot;Enter students name\n&quot;);
gets(*s_ptr);
printf(&quot;Enter course student has enrolled in\n&quot;);
gets(*c_ptr);
s_ptr++; c_ptr++;
}
*s_ptr = '\0'; /*add null string*/
c_ptr = course;
for(s_ptr=student; s_ptr ; s_ptr++)
{
puts(*s_ptr);
puts(*c_ptr);
c_ptr++;
}
}

What I would like to be able to do is to create a multi dimensional string table to store both students name and course, like so.

char student_details[MAX][30][30] = {
{&quot;Joe Bloggs&quot;, &quot;History&quot;},
{ &quot;ect&quot;, &quot;ect&quot; }
}
The only trouble is that all examples I have in course material only cover string tables like these which have been initialized with entries. I would like to allow user to enter names and course details straight in to this table,and then display the tables, either by conventional means or by pointers. The truth is i have no idea how to go about this any help will be appreciated. Try to remember this is course material, and I have only to use functions we have covered so far, so this excludes function calls, files or malloc. I have just completed pointers (though not in great detail.
Thanks in advance,
bigtamscot Hoping to get certified..in C programming.
 
To use a multi dimensional array the coventional way, declare the array as you have done...
char student_details[MAX][30][30]

then to enter them directly into the array from the keyboard...
for(x = 0;x < MAX;x++)
{
printf(&quot;\nEnter student name > &quot;);
gets(student_details[x]);
}

hope this helps you get on the right track.
 
if you're allowed to, do it like this:

#include <stdio.h>
#include <conio.h>

#define MAX 10

typedef struct Details {
char name[30];
char course[30];
} StudentDetails;

int main() {
int i;
StudentDetails sd[MAX];

/* read in values */
for(i = 0; i < MAX; i++) {
printf(&quot;Enter Name: &quot;); gets(sd.name);
printf(&quot;Enter Course: &quot;); gets(sd.course);
printf(&quot;\n&quot;);
}

printf(&quot;\n&quot;);

/* display */
for(i = 0; i < MAX; i++) {
printf(&quot;Info: %s, %s\n&quot;, sd.name, sd.course);
}

getch();
}

ps. you REALLY need more practice with pointers ;-)
 
Let me point out the mistakes in u'r prog:

char *s_ptr, *c_ptr; means one string only, it is in essence a 1-D array of chars.

s_ptr = student; c_ptr = course;

is absolutely WRONG, how can u equate a 1-D array of chars (in this case a pointer) to a 2-D array of chars ? While compiling, the compiler will SCREAM!! (or warn at least)

give a &quot;man gets&quot; and see the syntax for gets.

Conventionally the maximum anybody goes for in character handling is a 2-D array or pointer, advancing beyond that is thought of as messy, so follow the structure method suggested here, that is good programming. Trying to pull a &quot; char student_details[MAX][30][30]&quot; is a bad idea.
 
what are you talking about? there's nothing wrong with gets
 
gets() is a bug because it doesn't provide a way for the programmer to place a limit on how many characters the user can enter and will therefore happily overwrite past the end of the memory of the character pointer passed to it:

char buf[20];
gets(buf);

What happens if the user types in more than 20 characters? A program crash if you're lucky (or unlucky depending on how you look at it).

Many a C program has been exploited by taking advantage of this vulnerability, the most famous of which is probably the Internet Worm of 1988. In part, it caused a buffer overflow in the finger daemon, by sending more characters than it could hold. The finger daemon at the time used gets() to get characters from the user.

fgets() is a safe way of doing the same thing, because you can tell it the max characters it's allowed to store in your buffer:

char buf[20];

if (fgets(buf,sizeof buf,stdin)) {
/* ... */

fgets() will read no more than 1 character less than the value of its 2nd argument.

fgets() has slightly different behavior than gets() in that it retains the newline (whereas gets() discards it), unless the user entered more than the buffer can handle. So, to get this behavior you can do:

if (fgets(buf,sizeof buf,stdin)) {
char *tmp=strchr(buf,'\n');
if (tmp) {
*tmp=0;
} else {
/* user input was truncated */
}
} else {
/* input failed completely */
}

See also:


Try a google search on:

&quot;buffer overflow&quot; &quot;gets function&quot;
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top