Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ArchMiffo

Need help

This topic is 6079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, whats wrong with this function (apart from the fact that the string will be in reversed order) void IntToString(int i, char *str) { int ii; while(i>0) { ii = (i%10) + ''0''; char c = ii; *str = c; str++; i -= i%10; i /= 10; } } It compiles without a problem but when I run the program I get an access violation message from the statment "*str = c;". I´m I missing something here? Any help would be kind

Share this post


Link to post
Share on other sites
Advertisement
For a start, you''re reinventing the wheel (assuming this isn''t just a learning exercise). There''s an IntToStr with Borland Builder and atoi for everything else.

First thing: is the string zero terminated? I can''t see where you bung a NUL in (''\0''), though if your buffer is filled beforehand then it''s okay.

Also, another important point: consider what you''re chucking in as the "str" parameter. If you do this:

char* pMyString;
IntToString(1000, pMyString);

Then you''re in trouble, because writing to your string pointer will cause bad things to happen.

Try doing this (untested):
char myString[100];
memset(myString, ''\0'', 100); /* clear yer string */
IntToString(1000, myString);

See if that blows up.

The line "*str = c;" should be fine - you''re saying with that "write the value of c into the current character of the string". You shouldn''t need to change that at all.

Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

This is probably a result of how you''re calling the function. If you do char *string; IntToString(10, string);

you will get this problem. Make sure you have enough memory allocated for the results.

Share this post


Link to post
Share on other sites
Update: I've tried compiling your code and it works fine. It's definitely what you're passing in as a parameter that'll be causing the problem. Make sure your string is big enough for the number and that it's zero terminated.

The test code:

#include <cstdio>
#include <cstring>

void IntToString(int i, char *str);

int main(int argc, char* argv[])
{
char string[100];
memset(string, '\0', 100);
IntToString(1530, string);

printf("string is %s\n", string);

return 0;
}

void IntToString(int i, char *str)
{
int ii;
while(i>0)
{
ii = (i%10) + '0';
char c = ii;

*str = c;
str++;
i -= i%10;
i /= 10;
}
}


Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

Edited by - Alimonster on February 23, 2002 11:45:47 AM

Share this post


Link to post
Share on other sites
Alimonster, thanks for the reply. To clear things out this is just an exercise I´m doing. In real life I hardly use char*-strings at all (the string class in STL is much better).
Back to the question at hand. The string I´m sending to the function has been initialised and should be able to hold all the chars. This problem is realy starting to bug me becouse when I tried this:

int main()
{
char *str = "hello";
char c = ''H'';

*str = c;

cout<
cin.get();

return 0;
}

I got the same error as before. Now correct me if I´m wrong but shouldn´t this work? After all I am dereferencing a pinter to a char and assigning the return a char, so what´s the problem?

Share this post


Link to post
Share on other sites
The problem is the line:
char *str = "Hello"; // <-- Evil! Evil! Nooooooooo!

Replace it with this:
char str[] = "Hello";

You should only use the "char *xyz = ..." syntax for strings you don''t want to change. Any other time, use array notation.

Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

Share this post


Link to post
Share on other sites
Alimonster, I tried your code, and it worked. The I tried my previous code with your code, that is I used char string[100] and then memset to null-terminate it. What I can´t understand is why it won´t work with a string that´s initialised like this: char *string = "Hello". Care to explain?

Share this post


Link to post
Share on other sites
No.

I only have a slight understanding of this, because I tend to steer clear of them.

When you say "char* ..." you are saying to the compiler that you want it to declare enough space for that string, and set the pointer to it. It seems the string is considered constant: any time you try to change it, you get an access violation. At least, that's how I interpret it.

However, you can change the actual pointed-to string itself, so code like this would be fine:

char* xyz = "hello";
char qqq = 'H';

xyz = &qqq; // now it's H, non-null terminated.

Of course, doing that with a char is a bad move because it won't be properly terminated. Generally, just stick to arrays when declaring your strings and forget about it.

Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

Edited by - Alimonster on February 23, 2002 12:09:05 PM

Share this post


Link to post
Share on other sites
Thanks Alimonster. Now I will never forget that . It´s a good thing I made this misstake, now I understand pointers and the old C-strings a lot better, and it have also given me one more reason to use the new string class

Edited by - ArchMiffo on February 23, 2002 12:11:59 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!