Folks,
Programming has been a life-long hobby. It was never my means to make a living - though I have written quite a lot of C (well, not much compared with some, but one program had about 8,000 lines of C, encompassing most C constructs). However, I moved to AutoHotKey about 10 years ago, and have just come back to C in the last few weeks - and I seem to have forgotten an alarming amount.
The below, however, is so simple that I can hardly believe I have forgotten that much. Also, it is a simplification of something I copied/pasted from a C forum. Their program gave me a segmentation fault, and I simplified it down to what you see below - but I still get the fault. Is this me, or is there something I'm not understanding about the C::B implementation?
#include <stdio.h>
#include <stdlib.h>
void main()
{
char *s = "ABCDEF"; // this line works. GDB shows s ...... 0x403024 _Jv_RegisterClasses+4206628>"ABCDEF"
temp = s[3]; // temp is assigned a value of 'D', which is what I expected
s[3] = 'h'; // this line gives a segmentation fault.
}
I tried replacing the offending line with ...
... but I still get the segmentation fault - yet if figure that the address of s[3] must be correctly resolved, because "temp = s[3];" works as expected.
I know this is a C::B forum, not a C forum, but would somebody please dig me out? I'm all at sea, trying to see what's wrong with my C.
I've made a discovery - if the first line of the program is modified, so the program becomes ...
void main()
{ char s[] = "ABCDEF"; // instead of char *s =
char temp;
temp = s[3];
*(s+3) = 'h';
s[2] = 'p';
}
... then it works. So with the C::B implementation, is a string not seen as an array of characters with a terminating \0?
Is this possibly due to an implementation that uses Unicode characters, or perhaps because it is seeing my program as something other than C (C++, C#, or something else)?
I'm sure I was able to address a character in a string as str[char_no] years ago, when I was using DJGPP and RHIDE (old freebie compiler and IDE/debugger, also based on GDB)