Monday, July 27, 2009

Can any C programmers help me wit an error i am getting?

this is a code i have written to fill a 2d array wit 6 names that can have a maximum length og 24, however i keep getting the error (error C2440: '=' : cannot convert from 'char [24]' to 'char')





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





char main ()


{


char name [24] ;


char list [6] [24];





for(int count=0; count%26lt;=6; count++)


{


scanf("%s", %26amp;name);





for(int val=0; val%26lt;=6; val++)


{


for(int val2=0; val2%26lt;=24; val2++)


{


list[val][val2]=name; (it indicates the error in this line)


}


}


}





printf(" the names entered is:""%s", list);





return 0;


}

Can any C programmers help me wit an error i am getting?
Is a common mistake to try to assign strings in C by using the assignment (=) operator.





Usually, to copy the content between arrays, is a better idea to copy one element at a time or to use the memory management functions.





C-style strings are also arrays (of char elements) where the terminating character is the NULL constant. This gives the C-style strings more processing possibilities. The C standard library offers a wide collection of functions to help woking with strings. Simply include the file %26lt;string.h%26gt; in your project to use these functions.





There are many ways to fix your program:





You should eliminate the for with the count and substitute the problematic lines by:


for(int val = 0; val %26lt; 6; val++)


{


/* read the name */


scanf("%s", name);





/* copy the read name into the list of names */


for(int val2 = 0; val2 %26lt; 24; val2++)


list[val][val2] = name[val2];


}





This should solve your problem.





You can try to read the names directly in the list like:





for(int val = 0; val %26lt; 6; val++)


scanf("%s", list[val]);





Or finally, you can use the C standard string library:





for(int val = 0; val %26lt; 6; val++)


{


/* read the name */


scanf("%s", name);





/* copy the read name into the list of names */


strcpy(list[val], name);


}





If you need a C++ class to encapsulate a C-style string, you can use the "string" standard class. You can also search the internet for some good libraries or go to my website negyware.110mb.com for some C++ libraries.





*****************





To print the list, simply use:





for(val = 0; val %26lt; 6; val++)


printf("%s", list[val]);
Reply:I haven't done any C in a long time, but it looks like you may be putting a numeric value into a string. Try 'cast double'ing it.





That's what you do in VB.net, I'm sure there's an equivalent in C
Reply:I'm pretty sure that line should be changed to





list[val][val2]=name[val2];





otherwise you are trying to fit a 24 size char into a single char slot.





you might also consider using string function strlen(name) or strlen(name)+1 as the limit in the inner for loop. (for which I think you need string.h)





good luck
Reply:Use Ruby. The buffer overflow argument would not show up.


No comments:

Post a Comment