sprintf: makes pointer from integer without a cast`

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

sprintf: makes pointer from integer without a cast`

 
0
  #1
Sep 6th, 2008
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:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. int len = 0;
  7.  
  8. char *str = "This is a string";
  9. char ch = '\0';
  10. int i = 0;
  11. len = strlen(str);
  12.  
  13. i = sprintf(ch, "%X", 0x0D);
  14. strcat(str, ch);
  15. i = sprintf(ch, "%X", 0x0A);
  16. strcat(str, ch);
  17.  
  18. len = strlen(str);
  19. printf("\n%s", str);
  20. printf("str is %d chars long\n", len);
  21.  
  22. return 0;
  23. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sprintf: makes pointer from integer without a cast`

 
0
  #2
Sep 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: sprintf: makes pointer from integer without a cast`

 
0
  #3
Sep 6th, 2008
Originally Posted by Salem View Post
Say char ch[10]; but make sure the array is big enough.
In this example, 10 is the index of a char array ch, but I don't want to modify the tenth element. Also, ch is a single char. I don't quite understand you mean here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sprintf: makes pointer from integer without a cast`

 
0
  #4
Sep 6th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: sprintf: makes pointer from integer without a cast`

 
0
  #5
Sep 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,046
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: sprintf: makes pointer from integer without a cast`

 
0
  #6
Sep 6th, 2008
>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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: sprintf: makes pointer from integer without a cast`

 
0
  #7
Sep 6th, 2008
>> but what else can I do to put in CR and LF to the end of strings?
simple
  1. char str[100] = "a string"; // see Salem's post #2 for this line
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

Re: sprintf: makes pointer from integer without a cast`

 
0
  #8
Sep 8th, 2008
Gee, I totally forgot about that, thanks, Ancient Dragon. I was trying to make it too difficult.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC