getting run time error(cant run this code)

MS Chaudhar

getting run time error(cant run this code)

by MS Chaudhar » Fri, 06 Aug 2010 13:48:13

void func(char* temp[])

void main()
{
char* temp={"0001110011\0","1110001100\0"};
func(temp);
}

void func(char* temp[])
{

cout<<temp[0];


}

Hello actually the scenario is that i want to pass array of strings to the function and want to use it for my purpose. It compiles fine but when i try to execute the prog. it gives mein error.

kindly help me out

thanks




rtpninj

getting run time error(cant run this code)

by rtpninj » Sun, 08 Aug 2010 14:49:14

Change the following line:

Code Snippet
char* temp = { "0001110011\0", "1110001100\0" };

to:

Code Snippet
char* temp[] = { "0001110011\0", "1110001100\0" };

-James


MS Chaudhar

getting run time error(cant run this code)

by MS Chaudhar » Tue, 10 Aug 2010 15:50:15

Thanks james for giving the reply. the main issue is when i try to change the value of temp at any character it gives me access viloation error. can u help me out.

the code is like this

void func(char* temp[]);


void main()
{
char* temp[]={"0001110011\0","1110001100\0"};
func(temp);
}

void func(char* temp[])
{

temp[1][3]='1';
cout<<temp[1];


}




Viorel

getting run time error(cant run this code)

by Viorel » Wed, 11 Aug 2010 17:52:17

I think the problem is caused by the fact that your array points to literal strings that are stored in a write-protected area. For example, if you try a simpler fragment like this:

char * s = "abc";

s[1] = 'd';

then you will see the same run-time error. The problem can be solved by storing your strings in a write-allowed area: e.g. on "stack" or on "heap" allocated with new etc.

Since the above sample works if is changed to:

char s[] = "abc";

maybe it is suitable to you to change the definition of variable and argument to something like this:

void func(char temp[][20]);

and

char temp[][20] = { "0001110011", "1110001100" };

Now it is allocated on writable stack. Also consider using of std:: string and std:: vector classes, which allocate data on stack and heap.

I hope this makes sense.


Ronnie9

getting run time error(cant run this code)

by Ronnie9 » Thu, 12 Aug 2010 19:54:19

char* temp[]={"0001110011\0","1110001100\0"};
you definde a pointer array with length = 2, temp[0], tem[[1].
so there is no two dimension array and temp[1][3] is not exist.

if you want to access the char in the string, you can do it like this:
char* zTemp = temp[1] or zTemp = temp[0]
but you still can not write someting to any elments zTemp[ i ] (i = 0, ....9), because they are in the non-writable memory area,just like
Viorel
said.
.


rtpninj

getting run time error(cant run this code)

by rtpninj » Fri, 13 Aug 2010 18:53:18

Ah, okay. The version that you originally posted wouldn't compile, so I changed the one line, but now you're trying to write to a string literal, I didn't see that in the original example. I agree with what the other poster has said. Allocate the strings on the heap (for example) and you shouldn't have a problem.

James