Read 2 Byte of a Pointer

This is a discussion on Read 2 Byte of a Pointer within the C++ Programming forums, part of the General Programming Boards category; Hi everyone I have an operation in my programm where i have to copy parts of a pointer pointing to ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    Read 2 Byte of a Pointer

    Hi everyone

    I have an operation in my programm where i have to copy parts of a pointer pointing to an array of chars into another char at another pointers address.

    Declaration:
    Code:
    struct EXIF
    {
    
    	char DateTimeOriginal[20];		
    	char Year[4];
    	char Month[2];
    	char Day[2];
    	char Hour[2];
    	char Minute[2];
    	char Second[2];
    	...
    };
    The function:

    Code:
    for (int a = 0; a <= 3; a++)
    {		
       pEXIF->Year[a] = pEXIF->DateTimeOriginal[a];
    
       ...
    }
    However this just copies the whole string into the other array but i just want the first 4 characters of pEXIF->DateTimeOriginal stored into pEXIF->Year.
    I'm not that firm in pointer stuff so I hope one of you has a tip for me

    greetings

    ghost

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by ghostcoder View Post
    Hi everyone
    Code:
    for (int a = 0; a <= 3; a++)
    {		
       pEXIF->Year[a] = pEXIF->DateTimeOriginal[a];
    
       ...
    }
    However this just copies the whole string into the other array but i just want the first 4 characters of pEXIF->DateTimeOriginal stored into pEXIF->Year.
    I'm not that firm in pointer stuff so I hope one of you has a tip for me
    Why does the topic says about 2? It indeed copies the first 4 characters, but you probably tried to display this year and it displayed something else. It did, because the Year has no null-terminating character, which ends the string.

    Code:
    struct EXIF {
    ...
    char Year[5];
    ...
    };
    
    
    for (int i = 0; i < 4; ++i)
    {		
       pEXIF->Year[i] = pEXIF->DateTimeOriginal[i];
    }
    pEXIF->Year[4] = '\0';
    I never put signature, but I decided to make an exception.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to copy just four characters, you need to do this:
    Code:
    for (int a = 0; a <= 3; a++)
    {		
       pEXIF->Year[a] = pEXIF->DateTimeOriginal[a];
    }
    Yes, that's what you have. Yes, that's what it does. I don't know why you think the rest of it is in there, since Year only has room for four characters in it.

    Now, you do appear to have made a deliberate design choice to make all your data non-printable. That is to say, there's no built-in function, such as >>, that will print any of your data fields. So you will have to write a custom print function (overloading >> isn't possible here) if you want to actually look at the data on the screen. (OR, you could reverse the original design decision, and make each of your char arrays one byte longer to handle the \0 character at the end which would turn your char arrays into C-style strings. OR, you could just use C++ strings.)

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Thank you all for your replies.
    The NUL termination mehtod be expanding the arrays worked but for structural reasons I exchanged them by C-strings.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by ghostcoder View Post
    Thank you all for your replies.
    The NUL termination mehtod be expanding the arrays worked but for structural reasons I exchanged them by C-strings.
    Err, those are C-strings. Perhaps you mean you decided to use C++ strings?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read from a file into an array or pointer
    By Rataplar in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2010, 08:45 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Struct (with pointer) to byte array
    By rabencor in forum C Programming
    Replies: 1
    Last Post: 02-08-2009, 04:27 AM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21