User forums > Using Code::Blocks
Have I forgotten C?
mijewen:
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?
--- Code: ---#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.
}
--- End code ---
I tried replacing the offending line with ...
--- Code: ---*(str+3) = 'h';
--- End code ---
... 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.
mijewen:
I've made a discovery - if the first line of the program is modified, so the program becomes ...
--- Code: ---void main()
{ char s[] = "ABCDEF"; // instead of char *s =
char temp;
temp = s[3];
*(s+3) = 'h';
s[2] = 'p';
}
--- End code ---
... 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)
stahta01:
Read the rules.
scarphin:
Please be aware that there is no cb implementation of any compiler ever. Your problem is a 'trying to change contents of constant memory' problem as in this line:
--- Quote from: mijewen on July 10, 2014, 01:30:55 pm ---
--- Code: ---char *s = "ABCDEF"; // this line works. GDB shows s ...... 0x403024 _Jv_RegisterClasses+4206628>"ABCDEF"
--- End code ---
--- End quote ---
you instantiate a pointer pointing to a 'constant'.
Programming errors like this one have nothing to do with CB and to be sure if the problem is a programming error or related with cb, one should try executing (or compiling) the code with same options from the command line and check if it works.
mijewen:
Thanks you, scarphin.
stahta01 - which rules are you referring to, and where can I read them?
Navigation
[0] Message Index
[#] Next page
Go to full version