how to access higher char array element?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mwebel@freenet.de

    #1

    how to access higher char array element?

    Hi!,
    im trying to copy the middle part of a dinamycally created char* string
    with memcopy but all i get is rubbish...
    I understand, that malloc can allocate memory wherever it wants and it
    does not have to be in one piece... but how do i access higher parts of
    the allocated memory?

    lets say source contains "helloworld " and i only want to have "llowo"
    in destination... what do i do?

    char* destination=mal loc(5);
    char* source=malloc(8 );
    [.....]

    memcpy(destinat ion, source +3 ,5);

    this line copies whatever.... and writing source[3] does not work
    either... a hint would be nice... :D
    thanks for any help!

  • Richard Heathfield

    #2
    Re: how to access higher char array element?

    mwebel@freenet. de said:
    Hi!,
    im trying to copy the middle part of a dinamycally created char* string
    with memcopy but all i get is rubbish...
    I'm not familiar with memcopy. Do you mean memcpy?
    I understand, that malloc can allocate memory wherever it wants and it
    does not have to be in one piece...
    You understand wrongly. Each call to the malloc function yields either a
    null pointer (in the case of failure) or a pointer to a contiguous region
    of memory of at least the size requested.
    lets say source contains "helloworld " and i only want to have "llowo"
    in destination... what do i do?
    >
    char* destination=mal loc(5);
    char* source=malloc(8 );
    The region pointed to by source does not have sufficient storage to store
    the string "helloworld ". It would require 11 bytes, not 8.
    [.....]
    >
    memcpy(destinat ion, source +3 ,5);
    >
    this line copies whatever.... and writing source[3] does not work
    either... a hint would be nice... :D
    It is probable that you are forgetting about the importance of a null
    terminator, but we can't tell from what you've shown so far. Post the
    smallest *complete* C program that demonstrates the difficulty you are
    having, and I have little doubt that we will be able to point out the
    problem.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • markpapadakis

      #3
      Re: how to access higher char array element?


      mwe...@freenet. de wrote:
      Hi!,
      im trying to copy the middle part of a dinamycally created char* string
      with memcopy but all i get is rubbish...
      I understand, that malloc can allocate memory wherever it wants and it
      does not have to be in one piece... but how do i access higher parts of
      the allocated memory?
      >
      lets say source contains "helloworld " and i only want to have "llowo"
      in destination... what do i do?
      >
      char* destination=mal loc(5);
      char* source=malloc(8 );
      [.....]
      >
      memcpy(destinat ion, source +3 ,5);
      >
      this line copies whatever.... and writing source[3] does not work
      either... a hint would be nice... :D
      thanks for any help!
      Perhaps I am misunderstandin g your intentions here, but why something
      like:

      char *destination = (char *)malloc(6), *source =
      strdup("hellowo rld");


      memcpy(destinat ion, source + 2, 5);
      destination[5] = '\0';

      won't work for you?
      Perhaps you need to consider the following:
      o you need to allocate length + 1, because the string is null
      terminated
      o offsets base is 0, not 1. Thus you should memcpy() from source + 2
      o you need to set the '\0' on the destination ( null character
      terminator ).

      Mark Papadakis

      Comment

      • Martin Ambuhl

        #4
        Re: how to access higher char array element?

        mwebel@freenet. de wrote:
        Hi!,
        im trying to copy the middle part of a dinamycally created char* string
        with memcopy but all i get is rubbish...
        I understand, that malloc can allocate memory wherever it wants and it
        does not have to be in one piece... but how do i access higher parts of
        the allocated memory?
        >
        lets say source contains "helloworld " and i only want to have "llowo"
        in destination... what do i do?
        >
        char* destination=mal loc(5);
        Obviously wrong. There are 6 chars in "llowo".
        char* source=malloc(8 );
        Obviously wrong. There are 11 chars in "helloworld ".
        [.....]
        >
        memcpy(destinat ion, source +3 ,5);
        Obviously wrong. The chars {'l','l','o','w ','o'} begin at source+2.
        >
        this line copies whatever.... and writing source[3] does not work
        either... a hint would be nice... :D
        "Does not work" doesn't work as a description. Below you will see a
        demonstration that everything you claim "does not work" does work when
        done correctly.


        Are you perhaps trying to use the non-string array
        {'l','l','o','w ','o'} as if it were a string?

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        int main(void)
        {
        char source[] = "helloworld ";
        char *dest;
        size_t i;

        printf("Copying the 5 chars from \"%s\"[2]\n", source);
        if (!(dest = malloc(5))) {
        printf("malloc failed.\n");
        exit(EXIT_FAILU RE);
        }
        memcpy(dest, source + 2, 5);
        for (i = 0; i < 5; i++)
        printf("%u %#3o %c\n", (unsigned) i, (unsigned) dest[i],
        dest[i]);
        putchar('\n');
        free(dest);

        printf("Creatin g a string using the 5 chars from \"%s\"[2]\n",
        source);
        if (!(dest = malloc(6))) {
        printf("malloc failed.\n");
        exit(EXIT_FAILU RE);
        }
        memcpy(dest, source + 2, 5);
        dest[5] = 0;
        for (i = 0; i < 6; i++) {
        if (dest[i])
        printf("%u %#3o %c\n", (unsigned) i, (unsigned) dest[i],
        dest[i]);
        else
        printf("%u %#3o %s\n", (unsigned) i, (unsigned) dest[i],
        "(zero byte)");
        }
        printf("The created string: \"%s\"\n", dest);
        free(dest);

        return 0;
        }


        Copying the 5 chars from "helloworld "[2]
        0 0154 l
        1 0154 l
        2 0157 o
        3 0167 w
        4 0157 o

        Creating a string using the 5 chars from "helloworld "[2]
        0 0154 l
        1 0154 l
        2 0157 o
        3 0167 w
        4 0157 o
        5 0 (zero byte)
        The created string: "llowo"

        Comment

        • Martin Ambuhl

          #5
          Re: how to access higher char array element?

          markpapadakis wrote:
          Perhaps I am misunderstandin g your intentions here, but why something
          like:
          >
          char *destination = (char *)malloc(6), *source =
          strdup("hellowo rld");
          If his intention is to write standard C, then there is a good reason not
          to use the above: strdup does not exist in C and if it ever does there
          is no guarantee that the standard strdup will behave like yours.

          Comment

          • mwebel@freenet.de

            #6
            Re: how to access higher char array element?


            Martin Ambuhl schrieb:
            [a nice example]


            Thanks to all who answered...
            indeed the error came from me missallocating the space and trying to
            read from non allocated memory! i also didnt take into account the last
            0...

            indeed im writing in c in windows.
            I actually do c++ and had to do this one thing for compatibiltys sake
            with a programm im using.

            thanks again to all!!

            Comment

            Working...