S
Scott Lyons
Hey all,
Can someone help me figure out how to pass a dynamic array into a function?
Its been giving me some trouble, and my textbook of course doesnt cover the
issue. Its probably something simple, but its just not popping into my mind
at the moment.
My little snippet of code is below. Basically, the studentID[] array is
dynamic so it will fit any length of a Student's Name. What I'm trying to
do is place this chunk of code into a function, but I'm messing up the
pointers somewhere along the way. The function would need return
studentID[] for further use(I have 1 other dynamic arrays that uses it).
If anyone with a bit more expertise then myself could take a look, it would
be great! I'm going to keep at this thing for awhile more, so I'll check
for replies later in the evening. I've spent a good amount of time on this
sucker and I want to get it myself...lol. But if I dont, hopefully I can
pick up a hint or two here
Thanks in advance!
Scott
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#define NUM_STUDENTS 5
int main()
{
char buffer[81];
char *studentID[NUM_STUDENTS];
for (int i = 0; i < NUM_STUDENTS; ++i)
{
cout << "Enter the student's ID number: ";
cin.getline(buffer,81);
cout << endl;
studentID = new char [strlen(buffer) +1];
strcpy(studentID, buffer);
}
return 0;
}
Can someone help me figure out how to pass a dynamic array into a function?
Its been giving me some trouble, and my textbook of course doesnt cover the
issue. Its probably something simple, but its just not popping into my mind
at the moment.
My little snippet of code is below. Basically, the studentID[] array is
dynamic so it will fit any length of a Student's Name. What I'm trying to
do is place this chunk of code into a function, but I'm messing up the
pointers somewhere along the way. The function would need return
studentID[] for further use(I have 1 other dynamic arrays that uses it).
If anyone with a bit more expertise then myself could take a look, it would
be great! I'm going to keep at this thing for awhile more, so I'll check
for replies later in the evening. I've spent a good amount of time on this
sucker and I want to get it myself...lol. But if I dont, hopefully I can
pick up a hint or two here
Thanks in advance!
Scott
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#define NUM_STUDENTS 5
int main()
{
char buffer[81];
char *studentID[NUM_STUDENTS];
for (int i = 0; i < NUM_STUDENTS; ++i)
{
cout << "Enter the student's ID number: ";
cin.getline(buffer,81);
cout << endl;
studentID = new char [strlen(buffer) +1];
strcpy(studentID, buffer);
}
return 0;
}