Sunday, August 2, 2009

Storing an array of names to check against - c programming?

Here is my code, it needs to check against a possible names on the list , but it isn't compiling . Can you please tell me what the reason to this is ?





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main() {


int name[] = {"EDDIE","BEN","DAVE"};





char guess[10];


int correct = 0;


char * p;





printf("Enter a name in uppercase: ");





while(!correct) {


fgets(guess ,10, stdin);


p = strchr(guess, '\n');


if ( p )


*p = 0;





if(strcmp(name, guess)==0) {


printf("Correct!\n");


correct = 1;


}


else {


printf("Try again: ");


}


}


}

Storing an array of names to check against - c programming?
I'm too tired right now to go through your whole program, but what stands out is the way you have declared the name[] array.


The name[] array should be 'char' or 'unsigned char' and not 'int.'





Arrays are a collection of a certain data type and each array element are of the same data length.


The line below is an array of 1 byte signed data which consists of the ASCII number equivalents of "a", "b", and "c":


char fuzz={"a","b","c"};





In the case of strings, it is possible that each string element in the array may be of a different length, so you have to be careful when you declare an array of strings. There are 2 ways to declare an array of strings.





One method is like this:


char *name[]={"Hello"," out", "there"};


Notice the * in front of name[]. This means that I'm actually declaring an array of char pointers, but each pointer is initialized to point to 3 strings. The compiler creates 2 sets of data; There are the 3 data strings and there are 3 memory address pointers which point to their respective strings. For example, the first pointer (name[0]) points to the start of the "Hello" string.


The long way of accomplishing the method above would be something like this:


char *name[3]; // an array of 3 char pointers


char ptr1[]="Hello"; // declare 3 char strings:


char ptr2[]="out";


char ptr3[]="there.";





(inside main() procedure)


name[0]=ptr1; // name[0] points to "Hello"


name[1]=ptr2; // etc.


name[2]=ptr3;








The second method forces you to specify the length of each string. You have to do this because each element in the array must be the same size.


Example:


char name[][6]={"Hello","out","there"};


or


char name[3][6]={"Hello","out","there"};





The [6] means that each string element is 6 chars long. If one of the strings is less than 6 chars long, then there will be left over data at the end of the string. C/C++ strings are always zero-terminated, so the extra bytes after "out" is no big deal. The [3] in the 2nd example means that I am specifying that there are 3 strings in the array.


As you can see, this method may waste some memory space since each string element must be the same length.
Reply:*i know C++ programming, but both r quite similar, so tryin to answer anyway*





int name[] = {"EDDIE","BEN","DAVE"};





not sure what ur doin is this line, u put strings into a int array.


im guessin thats ur prob.


change it and try again.


also if u make it a char array it has to be a 2D array, and indent the braces properly, makes it much easier to read and check.
Reply:Expanding on the previous answer, yes, remember C does not have a string data type - though C++ does. A string in C is nothing more than a char array





char* str = "EDDIE\0";





there are numerous was to initialize a char array (string) in C. Remember C strings must also be null terminated. What you are attempting to do is really a 2D ragged array - an array of char pointers.


No comments:

Post a Comment