Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Error when trying to access character array

This topic is 4524 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

char* tmp = "Test\0";
int len = strlen(tmp), i;

for(i=0; i<len; i++) {
	char c = tolower(tmp);
	tmp = c;
}
On the line 'tmp = c', it gives me an access error; at this point i=0.

Share this post


Link to post
Share on other sites
Advertisement
"Test\0" is a constant that exists in the data segment of your program. You assign its memory address to a pointer. You should not change the data that pointer points to (though I'm suprised it stops you).

Try using std::string, or copy the data into a space you can change.

Share this post


Link to post
Share on other sites
Well unfortunately I'm in C, so std:: doesn't exist. Here's what I'm actually trying to do:


void lowerStr(char* str) {
int len = strlen(str), i;

for(i=0; i<len; i++) {
char c = tolower(str);
str = c;
}
}
...

char *str[] ={ "GTATAGGATCCACTGTGTTACAAGGATCCAATAC\0",
"agacgagttgtactgctgacacaggaaatggatgctgga\0" };

lowerStr(str[0]);

Share this post


Link to post
Share on other sites
it seems you have a mismatch occuring. i could be wrong though.

you have:

char *str[] = {"Hello", "World"};
void lowerStr(char *str); // this is expecting a str[] NOT *str[]


if i was you, i'd printf(%d, len); right before the loop, to make sure you getting the correct length. at this point, i don't think you are.


edit: matter of fact just make:

char *str[] = {"Hello", "World"};
//into
char str[] = {"Hello", "World"}; //notice no pointer symbol

Share this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
Well unfortunately I'm in C, so std:: doesn't exist.


The problem and solution remain. You're trying to write into read-only memory. You need to either use a copy algorithm or copy the string and modify the copy in place.

You're in C, so use calloc to allocate a new string buffer of the right length, then have your lowerStr() function copy the source string into the newly alloc'd space while converting it.

Share this post


Link to post
Share on other sites
How about this:

void lowerStr(char* str)
{
int len = strlen(str), i;

for(i=0; i<len; i++)
{
char c = tolower(str);

str = c;
}
}

...

// Copy the data somewhere we can use it...

char str[45];

strcpy( str, "HELLOTHISISATESTSTRING" );

lowerStr( str );



I have not tried that, but it should work. This is why C is an archaic language.

Share this post


Link to post
Share on other sites
There's really nothing wrong with your function code, just this:

isn't

char* tmp = "whatever"


be

char tmp[] = "whatever"


I don't know about your compiler but

char* tmp = something looks like your trying to assign a value to a pointer, not a string to an array.

edit: correct me if im wrong

Share this post


Link to post
Share on other sites
Writing something like
char* temp = "Test\0";

I think is equivalent to
const char* temp = "Test\0";

So that you can't fiddle with data that temp is pointing to.

But I think geekalert is right that you should do
char temp[] = "Test\0";

because then you putting the string into a simple character array

You may not need the \0 at the end of the string because I think the compiler puts one in for you...

Edit:
You could try this out

char temp[]="Test";
char* idx = temp;
while(*idx)
{
*idx = *idx|32;
idx++;
}


This is untested but it should work using ASCII set because the difference between A and a or H and h is bit 32(0 in upper case, 1 in lower case)

Share this post


Link to post
Share on other sites
That is an interesting question actually. If you go

char x[]="xyz";

in global space or whatever, does the compiler put "xyz" in data segment then ALSO create a x[] array somewhere you can modify and copy "xyz" in?

Or is the above just the same as

char *x="xyz";

Just curious.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Sfpiano
char *str[] ={ "GTATAGGATCCACTGTGTTACAAGGATCCAATAC\0",
"agacgagttgtactgctgacacaggaaatggatgctgga\0" };


Interesting... are you building a DNA-parser or something?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!