Re: Pointer Declaration/Array definition

Jens.Toerring_at_physik.fu-berlin.de
Date: 08/22/04


Date: 22 Aug 2004 16:41:34 GMT

ur8x@ur8x.com wrote:
> Jens.Toerring@physik.fu-berlin.de wrote:

Please be so kind not to top-post.

> Ok, here is what I want to know: What exactly happens when
> p[i] is called, as far accessing and dereferncing that makes
> the code wrong (yes, I know it should not work, I just want
> to know why).

In the process of compiling and linking the symbol 'p' will
get replaced by a certain memory address. The code in file2
knows that at this address there's a string, e.g. "ABCDEFG".
But the code in file1 assumes that at that address a pointer
to char is stored. Since you have "ABCDEFG" at that address
the code in file1 will interpret this value stored there as
an address like 0x61626364' (assuming you have 4 byte char
wide addresses on a big-endian machine and ASCII charset, so
0x61 == 'A' etc.). But that's of course no address but just
the bit pattern of the start of the string. If you then use
'p[i]' it tries to dereference that address (0x61626364 + i),
an address to which you proably have no access to and thus
you get a segmentation fault.
                                   Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • breaking up a String into an array of chars and adding to datatable
    ... I have a string that I am trying to add each char to a datatable row. ... for example if I have a string that looks like "abcdefg", ...
    (microsoft.public.dotnet.languages.vb)
  • Re: breaking up a String into an array of chars and adding to datatable
    ... Dim d As DataTable = New DataTable ... Dim line As String = "abcdefg" ... array of Char' cannot be converted to '1-dimensional array of Object' ...
    (microsoft.public.dotnet.languages.vb)
  • c++ newbie question
    ... how can i read a string from a text file in c++ using fstream.h? ... how can i read that as char* or char? ...
    (comp.lang.cpp)
  • [PATCH 09/21] perf: rewire generic library stuff, p5
    ... +int eprintf(int level, const char *fmt, ...) ... * Helper function for splitting a string into an argv-like array. ... +static int count_argc(const char *str) ...
    (Linux-Kernel)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)