| | |
sprintf: makes pointer from integer without a cast`
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main(void) { int len = 0; char *str = "This is a string"; char ch = '\0'; int i = 0; len = strlen(str); i = sprintf(ch, "%X", 0x0D); strcat(str, ch); i = sprintf(ch, "%X", 0x0A); strcat(str, ch); len = strlen(str); printf("\n%s", str); printf("str is %d chars long\n", len); return 0; }
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.
Say
Also, you can't do this either
Use 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"; Last edited by Salem; Sep 6th, 2008 at 3:44 pm. Reason: more code mistakes spotted
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
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
>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
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. "If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
>> but what else can I do to put in CR and LF to the end of strings?
simple
simple
C Syntax (Toggle Plain Text)
char str[100] = "a string"; // see Salem's post #2 for this line strcat(str, "\r\n");
Last edited by Ancient Dragon; Sep 6th, 2008 at 11:10 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- makes pointer from integer without a cast (C)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
Other Threads in the C Forum
- Previous Thread: please help!! have some problems with my address book
- Next Thread: sortied list
Views: 1246 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






