23 Jun, 2009, Banner wrote in the 1st comment:
Votes: 0
make -s smaug
Compiling o/editor.o….
editor.c: In function 'EDITOR_DATA* str_to_editdata(const char*, short int)':
editor.c:325: error: assignment of read-only location '*(eline->editor_line::line + ((unsigned int)((unsigned int)i)))'
editor.c:341: error: assignment of read-only location '*(eline->editor_line::line + ((unsigned int)((unsigned int)i)))'
editor.c:349: error: invalid conversion from 'const void*' to 'void*'
editor.c:349: error: initializing argument 1 of 'void* realloc(void*, size_t)'


I've gotten it down to several errors that look like this, so I figure I can fix the rest if I can get help with these two.


defines:
const char *p;
EDITOR_DATA * edd;
EDITOR_LINE * eline;
short i;
short tsize, line_count;


code:
if( max_size != NOLIMIT && tsize + line_count * 2 + 1 >= max_size )
break;

if( *p == '\r' )
;
else if( *p == '\n' )
{
eline->line[i] = '\0'; // 325
eline->next = make_new_line( "" );
eline = eline->next;
line_count++;
i = 0;
}





defines: 
const char *p;
EDITOR_DATA * edd;
EDITOR_LINE * eline;
short i;
short tsize, line_count;

#define RESIZE_IF_NEEDED( buf, buf_size, buf_used, added_use ) \
if( (buf_used) + (added_use) >= (buf_size) ) \
{ \
short added_size; \
added_size = BLOCK_ROUNDUP( added_use ); \
if( added_size == 0 ) \
added_size = CHAR_BLOCK; \
RECREATE( (buf), char, buf_size + added_size ); \
(buf_size) += added_size; \
}


code:
eline = eline->next;
line_count++;
i = 0;
}
else
{
eline->line[i] = *p; //341
eline->line_used++;
tsize++;
i++;
RESIZE_IF_NEEDED( eline->line, eline->line_size, eline->line_used, 1 ); // 349
}
p++;

}


if( eline->line[0] != '\0' )

{

eline->line[i] = '\0';


If you need more, simply ask.
23 Jun, 2009, Banner wrote in the 2nd comment:
Votes: 0
And right after I posted I figured it out…

const char *p;

should be:
char *p;


Mi mistake in massively converting it.
23 Jun, 2009, David Haley wrote in the 3rd comment:
Votes: 0
Hrm. Nothing you showed involves assigning to p, so I'm not sure why that fixed it, but good that it's working now… :smile:
23 Jun, 2009, Banner wrote in the 4th comment:
Votes: 0
David Haley said:
Hrm. Nothing you showed involves assigning to p, so I'm not sure why that fixed it, but good that it's working now… :smile:

It was in the defines above the actual code, after the error boxes.

Banner said:
#
# defines:
# const char *p;
# EDITOR_DATA * edd;
# EDITOR_LINE * eline;
# short i;
# short tsize, line_count;
#
# #define RESIZE_IF_NEEDED( buf, buf_size, buf_used, added_use ) \
# if( (buf_used) + (added_use) >= (buf_size) ) \
23 Jun, 2009, David Haley wrote in the 5th comment:
Votes: 0
What I meant is that nothing in the code assigns to p even after expanding the macro. Also, for that matter, p isn't used for anything except a dereference, so pointer constness doesn't matter.
23 Jun, 2009, Banner wrote in the 6th comment:
Votes: 0
char *buf;
const char *src, *tmp;

short size, used, i;


CREATE( buf, char, MAX_STRING_LENGTH );

size = MAX_STRING_LENGTH;

used = 0;

buf[0] = '\0';



I got an error with *buf here that was the same case as the error with *p, I had buf defined as const char *buf and it said something about read-only assignment when it got down to buf[0] =. I'm guessing assigning something as a const means it's constant, read-only, and it's not going to change, so when it attempted to assign something to it it got confuzzled?
23 Jun, 2009, David Haley wrote in the 7th comment:
Votes: 0
Yes, in the code you just showed, buf[0] = X is illegal if buf is a const char*. It means that it's a pointer to constant characters, meaning that you can't change the characters in the string. Changing buf to char* would indeed fix the problem shown in line 14 of your most recent code paste.
23 Jun, 2009, Banner wrote in the 8th comment:
Votes: 0
David Haley said:
Yes, in the code you just showed, buf[0] = X is illegal if buf is a const char*. It means that it's a pointer to constant characters, meaning that you can't change the characters in the string. Changing buf to char* would indeed fix the problem shown in line 14 of your most recent code paste.

Generally speaking, you'd never use a const char assignment within a function since it's going to change at some point? They can be used in function definitions since it's usually passing an argument the character has already typed, and that is not going to change throughout the function?
23 Jun, 2009, David Haley wrote in the 9th comment:
Votes: 0
Well, it depends on what you're going to do with it. If you want to actually change some string, you can't use const. But if you just want to scan a string, you can (and should) use a const char* to walk over the string.

Any input to the function that the function won't be changing should also be const. In some cases, you have no choice in the matter, as with character input for example which is const at the caller's location, and so you must pass it as const to the function.
23 Jun, 2009, Banner wrote in the 10th comment:
Votes: 0
Well, that makes much more sense when you put it that way. Thank you.
23 Jun, 2009, David Haley wrote in the 11th comment:
Votes: 0
The const keyword is a funny one because it takes a while to wrap one's head around it, so it's typically confusing to newbies, but once you eventually get it, it's pretty awesome for many reasons. It took me a while to really appreciate it, but now I sorely miss it in other languages. :wink:
0.0/11