must i using const char for strcpy?Code:char x[] = "hello"; const char *y = "world"; strcpy(x, y);
or i using const char for making repeat of sentence world?
must i using const char for strcpy?Code:char x[] = "hello"; const char *y = "world"; strcpy(x, y);
or i using const char for making repeat of sentence world?
Indicates that the program finished successfully, whatever that means to you. Returning 0 would have the same meaning.Originally Posted by loserone+_+
Unless you are dealing with a freestanding implementation, or you are trying to re-interpret potentially inaccurate language in the text of the standard, the standard has always stated that the return type main shall be int.Originally Posted by esbo
No, but you should use const char* to point to the first character of a string literal. This makes it more likely that you will discover the mistake that you discovered in this thread.Originally Posted by loserone+_+
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok i finally understand all of your saying,
thanks for advice , this thread really helping me
lol i thought i solved the problem at all,
but when i am using scanf it getting program terminated,
here's my code,
whats wrong with scanf thing? must i using the thing -> & <- thing or not?Code:#include<stdio.h> #include<stdlib.h> #include<string.h> #define STR_MAX 100 main() {char x[STR_MAX],y[STR_MAX]; const char *r; printf("Your Name =");scanf("%2s",&r); strcpy(x,r); printf("Your Name Is =%2s",x);system("pause");return EXIT_SUCCESS;}
No, it's the same problem as before. You're not pointing at memory you can use(*).
Whenever you write a * in front of a variable, you need to also think about "OK, where is the memory coming from?".Code:char thisHasSpaceForAWord[100]; char *thisPointsToTheSameSpace = thisHasSpaceForAWord; char *thisPointsToSomeOtherSpace = malloc(100); // These two do exactly the same thing, with the same block of memory scanf("%s",thisHasSpaceForAWord); scanf("%s",thisPointsToTheSameSpace); // This works with the allocated memory, not the locally declared array scanf("%s",thisPointsToSomeOtherSpace);
(*)
OK, technically, &r is memory you can use, but it's total abuse of the concept, and you're stuck with very short strings.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
First, rewrite your program like this and then compile with warnings turned on
try.c: In function 'main':Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_MAX 100 int main(void) { char x[STR_MAX] = {'\0','\0','\0'}; char y[STR_MAX] = {'\0','\0','\0'}; const char *r; printf("Your Name ="); scanf("%2s", &r); strcpy(x, r); printf("Your Name Is =%2s", x); system("pause"); return EXIT_SUCCESS; }
try.c:11:2: warning: format '%s' expects argument of type 'char *', but argument
2 has type 'const char **' [-Wformat]
What's the problem?
1. The & will chang your variable r, which has type (const char *), to having type (const char **) which is wrong.
2. r does not point to anything valid. Contrast this to x and y, which are as you can see pointing to arrays of '\0' characters.
Possible fix:
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_MAX 100 int main(void) { char x[STR_MAX] = {'\0','\0','\0'}; char y[STR_MAX] = {'\0','\0','\0'}; char r[STR_MAX] = {'\0', '\0', '\0'}; printf("Your Name ="); scanf("%2s", r); strcpy(x, r); printf("Your Name Is =%2s", x); system("pause"); return EXIT_SUCCESS; }
lol, funny thing is when i using
from the fixed code before,Code:scanf("%2s",x)
It only print 2 character when program compiledCode:#include<string.h> #define STR_MAX 100 main() {char x[STR_MAX]; const char *r; printf("Your Name =");scanf("%2s",&x); printf("Your Name Is =%2s",x);system("pause");return EXIT_SUCCESS;}
so i change it to only
and it works better,Code:scanf("%s",x);
it seems i cant use strcpy from scanf pointer string like this
am i right? doesnt that just beat at all?Code:char x[20],*a; printf("Input Pointers =");scanf("%s",&a); strcpy(x,a);
lol i didnt know why i must wrote like thatCode:char x[STR_MAX] = {'\0','\0','\0'}; char y[STR_MAX] = {'\0','\0','\0'}; char r[STR_MAX] = {'\0', '\0', '\0'};
see my latest post, i fixed it but it seems i cant using scanf to pointer string like this
Code:char x[20],*a; printf("Input Pointers ="); scanf("%s",&a); strcpy(x,a);
When you write
scanf("%s", FOO);
FOO must have type (char *), not (const char *) and definitely not (char **).
You must make sure a points to valid memoryCode:char *a; scanf("%s", &a); // WRONG: &a means it is now (char **) scanf("%s", a); // OK: a is type (char *), but it doesn't point to anywhere valid.
Code:char mybuffer[10000] = {'\0'}; char *a = mybuffer; scanf("%s", a); // OK. a will not overflow as long as the input is no greater than 9999 characters, which is obviously not guaranteed.
You don't have to write it like that. It is to make the point that you are setting aside some memory with '\0' filled in. By default, if you include ANY initializer for an array, then all of the other elements (all the way up to STR_MAX-1) are filled in with '\0'. However, if you don't specify an initializer, then the initial contents are undefined.
it seems, using scanf with pointer string
it must had a hold with array,
okay i understand, but what is the meaning of
i am wanna know theCode:char mybuffer[10000] = {'\0'};
thing do, what is the meaning of that?Code:{'\0};
Normal array notation is like this
So in your case TYPE is char, SIZE is 10000 and all the elements are '\0'. The character '\0' is just a special character that has numerical value zero. In C all valid strings must be terminated by this character.Code:TYPE arrayname[SIZE] = {ELEM0, ELEM1, ELEM2, ...};
It means set aside a buffer of 10000 bytes, and initialize the buffer[0] to the value '\0'. The reason you want to explicitly initialize element 0 is following: If you forgot to initialize elements 1...9999, the compiler assumes you want to initialize the rest to 0, which is good.Code:char buffer[10000] = {'\0'};
Last edited by c99tutorial; 01-15-2013 at 05:58 AM.
the simple way u mean, it has zero value right?In C all valid strings must be terminated by this character.
Code:char buffer[10000] = {'\0'};
It means set aside a buffer of 10000 bytes, and initialize the buffer[0] to the value '\0'.
so it can hold 9999 value or 10000 value, thats ur mean right?
ohh yeah, why scanf is reading for string didnt know the space bar character, so it will printing only characters. It didnt recognize space command? It is because the compiler or my mistake?
An array with size 10000 can hold 10000 values. The elements are numbered starting from 0. a[0], a[1], a[2], ..., a[9999]. If the array is a string, then the end of the string must be marked with '\0'. In other words, the longest string you can have is 9999 characters, because the longest string must have '\0' in the 10000th spot (the 10000th spot is called a[9999]).
This will read each one up to the whitespace character, so if you have a space in it, it will treat them as separate words:
scanf("%s %s %s", str1, str2, str3);
An easy way is to use a different delimiter, say ;
scanf("%[^;];%[^;];%[^;];", str1, str2, str3);
That means each string may contain any number of spaces, and they must be each terminated by a ';' character. The strings themselves may not contain ';' characters.