Author Topic: Have I forgotten C?  (Read 3975 times)

Offline mijewen

  • Multiple posting newcomer
  • *
  • Posts: 19
Have I forgotten C?
« on: July 10, 2014, 01:30:55 pm »
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.
}

I tried replacing the offending line with ...

Code
*(str+3) = 'h';

... 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.
« Last Edit: July 10, 2014, 01:34:27 pm by mijewen »

Offline mijewen

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Have I forgotten C?
« Reply #1 on: July 10, 2014, 02:10:10 pm »
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';
}
... 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)
« Last Edit: July 10, 2014, 02:30:13 pm by mijewen »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: Have I forgotten C?
« Reply #2 on: July 10, 2014, 02:39:04 pm »
Read the rules.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Have I forgotten C?
« Reply #3 on: July 10, 2014, 03:24:11 pm »
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:
Code
char *s = "ABCDEF";  // this line works.  GDB shows  s ...... 0x403024 _Jv_RegisterClasses+4206628>"ABCDEF"
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.

Offline mijewen

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Have I forgotten C?
« Reply #4 on: July 10, 2014, 03:41:55 pm »
Thanks you, scarphin.

stahta01 - which rules are you referring to, and where can I read them?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: Have I forgotten C?
« Reply #5 on: July 10, 2014, 04:22:59 pm »
Please read the rules before you post!
http://forums.codeblocks.org/index.php/topic,9996.0.html
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org