4 Replies - 676 Views - Last Post: 09 February 2008 - 12:14 PM Rate Topic: -----

#1 ferenczi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 08

Why can't I do this whit characters in C?

Posted 09 February 2008 - 01:30 AM

Dear!


char *mov_dx;
	int decpnt, sign;
	char *premier;
	mov_dx = ecvt(xor_a, 5, &decpnt, &sign); /*till here everything OK*/

   /* the following line-code creates some errors */
	
	for (int v = 0; v < 5; v++) premier[v] = mov_dx[v];


How can I correct it? I'm working with C in DevC++

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Why can't I do this whit characters in C?

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Why can't I do this whit characters in C?

Posted 09 February 2008 - 01:52 AM

The error is coming from the variable "premier".
You have not allocated any memory for it so when the
pointer is dereferenced via premier[v]
you'll get an access violation.

change char *premier to char premier[5]
should fix it

Cheers

View Postferenczi, on 9 Feb, 2008 - 01:30 AM, said:

Dear!


char *mov_dx;
int decpnt, sign;
char *premier;
mov_dx = ecvt(xor_a, 5, &decpnt, &sign); /*till here everything OK*/

/* the following line-code creates some errors */

for (int v = 0; v < 5; v++) premier[v] = mov_dx[v];

How can I correct it? I'm working with C in DevC++

Thanks

Was This Post Helpful? 0
  • +
  • -

#3 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: Why can't I do this whit characters in C?

Posted 09 February 2008 - 01:54 AM

Well, it looks like you are trying to access two strings, but you haven't actually got anything inside the strings. So you are probably accessing protected memory.

Could you please post future code like this: :code: Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 ferenczi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 08

Re: Why can't I do this whit characters in C?

Posted 09 February 2008 - 02:51 AM

Someone told me I should allocate buffer. But I don't know how to do it. Can anyone explain me?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Why can't I do this whit characters in C?

Posted 09 February 2008 - 12:14 PM

There are essentially 2 ways to allocate memory. It can be done on the stack or dynamically. Lets take the error you are having with your code.

In your code you have this line char *premier.
This is a pointer to a character. Since you didn't initialize the variable
it is effectively pointing to garbage. You can't possibly know what
this variable is pointing. This means that you can't use it just yet.

The reason that this line for (int v = 0; v < 5; v++) premier[v] = mov_dx[v]; of code is failing is because the variable premier is
a char pointer not a char array;

To make this a character array you declare the variable like this instead
char premier[5] = {0}; . This will allocate 5 blocks of memory
on the stack of size char.

The other way to do it is like this:
char *premier;

// allocate space dynamically. 
premier = new char[5];

// now premier can be written to
 for (int v = 0; v < 5; v++) premier[v] = mov_dx[v];

....
// don't forget to clean up the dynamically allocated memory
delete [] premier



Hope that helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1