View Full Version : String array's? How can you???

05-10-2003, 05:28 PM
I'm working on a database program and I'm using array's to store the names entered into the program but seeing that Strings are already char arrays, it's become quite difficult. I'm having problems using 2 dimensional arrays to do this. Can anyone help? Here's the commented code, it's not too long. Thanks!

int main() //includes and such are above; no other functions yet. :)
int i = 0, namesEntered = 1, *nePtr; //integers to be used...
char name[ARRAY_MAX], nameInSlot[ARRAY_MAX][i]; //array's of chars.. "strings"

nePtr = &namesEntered; //namesEntered ("ne") pointer points to the address of the variable...

printf("How many names to enter: ");
scanf("%d", nePtr); //get the amount they entered.
printf("\n\n[%d character limit]\n", (ARRAY_MAX - 1));

for (i = 0; i <= (namesEntered - 1); i++)
printf("Enter name %d: ", (i + 1));
nameInSlot[ARRAY_MAX][i] = name[ARRAY_MAX]; //this SHOULD put the value of the string
//entered into a 2d array which consists of an string and
//and index to shove it into.... note the "SHOULD"


for (i = 0; i <= namesEntered; i++) //loop for output.
printf("%s\n", nameInSlot[ARRAY_MAX][i]); //ends up all it does is print "(null)" odd.
//or crashes the whole program. blah.
system("pause"); //press anykey...
return 0; //exit

05-11-2003, 03:57 PM
>> fflush(stdin);
This is not a good idea (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392)

Nor is this (http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044652485&id=1043284385)

>>char nameInSlot[ARRAY_MAX][i];
How big is i when this variable is declared? Answer: 0. Therefore, you've created an array like so:
Also, using variables within an array declaration is allowed (unless you're using C99 standard compiler). The array sizes have to be known at compile time.

>>nameInSlot[ARRAY_MAX][i] = name[ARRAY_MAX];
This does not copy a string, to do that you need strcpy().

Here's an adapted version to show you the way forward. It isn't by any means complete, but it should get your going in the right direction.

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_MAX 10
#define MAX_NAME_LENGTH 50

int main() //includes and such are above; no other functions yet.
int i;

for (i = 0; i < ARRAY_MAX; i++)
printf ("Enter name %d: ", i+1);
fgets(nameInSlot[i], MAX_NAME_LENGTH, stdin);


for (i = 0; i < ARRAY_MAX; i++)
printf("%s\n", nameInSlot[i]);

return 0;

05-12-2003, 04:33 PM
Thanks a lot, I'll be sure to make note of doing it that way! I actually figured a way how to do it with structures (arraying the structure string) after a little bit of posting this thread, which took up more space than you way. Unfortunatly I couldn't get back to the web site (Was it down??) to say I figured it out... (kept getting a DNS error).

Anyway, why is "fflush"ing the stdin after a scanf not good? I always thought you had to fflush your input after you used a scanf? (or fscanf) :confused: and why do you fflush the stdout after your printf? I read the page you linked to but it didn't really clear it up completely for me.... I didn't really see anything they flushed the input with.. the closest I saw was the "cout.flush()". (personally, I'm not a fan of "cin" and "cout"... it takes up too much room, well atleast the cout). ????????????????????????

05-13-2003, 03:41 PM
Ok, so I shouldn't use regular scanf if I can and just use sscanf? Thanks a lot for clearing that up, I'll look around the site for the syntax of sscanf. (that is, if it's different. :D)

Thanks again!