![]() |
| ||
| sprintf: makes pointer from integer without a cast` So I'm messing with some code, trying to get more familiar with string functions, and I stumbled into an apparent error on my part. Here is my code: #include <stdio.h> Those are hexadecimal values I'm trying to put in there. I understand that every ASCII character also has a decimal representation, but what else can I do to put in CR and LF to the end of strings? If I do what the error says, it would give the characters values I don't want, which would be 13, and 10, respectively. I've read various articles on the web about this, and each one has said to use sprintf, so what am I doing wrong? Thanks, I appreciate it. |
| ||
| Re: sprintf: makes pointer from integer without a cast` Say char ch[10];but make sure the array is big enough. Also, you can't do this either strcat( str, ch );str is a string constant (it may be in read-only memory). Modifying it will kill your program. Use say char str[100] = "a string"; |
| ||
| Re: sprintf: makes pointer from integer without a cast` Quote:
|
| ||
| Re: sprintf: makes pointer from integer without a cast` > 10 is the index of a char array ch, but I don't want to modify the tenth element. Who said that? It's an array of 10 chars. char ch[10]; sprintf( ch, "%x", value ); Simple. |
| ||
| Re: sprintf: makes pointer from integer without a cast` I knew that you could just use a normal char array, but I didn't know you couldn't modify a char array pointed to by a pointer. If you type *ptr, you can access the value at the variable the pointer points do, but I guess it can't be used in strcat since I tried it earlier before I posted. I'm practicing pointers, hence the purpose of this stupid program. I know they hold addresses to variables, and that they are useful to be passed as reference to functions so functions can modify the contents, but I'm still just a little foggy on what can be done with them. So once a pointer to an array is declared as in the *str line, it can't be modified. I got it now. Thanks Salem |
| ||
| Re: sprintf: makes pointer from integer without a cast` >So once a pointer to an array is declared as in the *str line, it can't be modified. I got it now. If you are referring to something like this char *str = "This is a string";you are correct. It is better to think of it as a string in read only memory. |
| ||
| Re: sprintf: makes pointer from integer without a cast` >> but what else can I do to put in CR and LF to the end of strings? simple char str[100] = "a string"; // see Salem's post #2 for this line |
| ||
| Re: sprintf: makes pointer from integer without a cast` Gee, I totally forgot about that, thanks, Ancient Dragon. I was trying to make it too difficult. |
| All times are GMT -4. The time now is 8:25 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC