char [150] doesn't fit into char[150] ?

This is a discussion on char [150] doesn't fit into char[150] ? within the C++ Programming forums, part of the General Programming Boards category; In my current project I have a union Code: union IFDEntry { unsigned char BYTE; unsigned char ASCII[150]; unsigned short ...

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

    char [150] doesn't fit into char[150] ?

    In my current project I have a union
    Code:
    union IFDEntry
    {
    	unsigned char BYTE;
    	unsigned char ASCII[150];
    	unsigned short int SHORT;
    	unsigned int LONG;
    	Bruch RATIONAL;
    	signed char SBYTE;
    	unsigned char UNDEFINED;
    	short int SSHORT;
    	int SLONG;
    	SBruch SRATIONAL;
    	float FLOAT;
    	double DOUBLE;
    };
    and a function with a pointer to an instance in memory
    Code:
    void ReadIFDEntry(unsigned long Entry, IFDEntry *pointer, FILE *pFile)
    within the function i try to apply a unsigned char [150] variable to the ASCII "part" of the union:
    Code:
    unsigned char Text[150];
    		
    [...]
    		
    *pointer->ASCII = Text;
    when compiling i get the following error:

    error C2440: '=': 'unsigned char [150]' canno be converted into 'unsigned char'

    I guess the solution is pretty easy but I have no idea at the moment.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Code:
    *pointer->ASCII = Text;
    Is identical to:
    Code:
    pointer->ASCII[0] = Text;
    You're trying to set a single character to the pointer to the string, which is obviously wrong. Look into strcpy.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,863
    In any event, if you want to copy arrays of char, use functions like strcpy() rather than assignment. The compiler is also required to complain about an assignment of the form "pointer->ASCII = Text;" as arrays are not copied that way.

    If you want to copy strings using assignment, use the standard string type (std::string from the <string> header) instead of arrays of char.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    thank you for your replys but unfortunately both solutions didn't worked out.

    if I use std:string the compiler doesn't accept this in a union structure since it has its own copy functions.

    If I use strcpy() with the char array, I get the following error:

    error C2664: 'strcpy': convertion of parameter 1 of 'unsigned char [150]' in 'char *' not possible

    I also tried to copy the string char by char with a for-loop but i get an access violation error

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    I also tried to copy the string char by char with a for-loop but i get an access violation error
    That should work.

    Show the code where you tried.

    As you have already stated strcpy() doesn't work on unsigned char and would also require the end of string terminator. But you should checkout memcpy().

    Jim
    Last edited by jimblumberg; 12-11-2010 at 09:44 AM.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    ok first here is the code i used for the char by char methode:

    Code:
    for (int a = 0; a < Count; a++)
    {
         fread(&Text[a], 1, 1, pFile);
        pointer->ASCII[a] = Text[a];
    }
    I get the following error from the debugger:

    PHP Code:
    Unhandled exception at 0x0114188f in NEF_Reader.exe0xC0000005:
    Access violation while writing at position 0x00000000
    I also tried memcpy with following code:

    Code:
    memcpy(pointer->ASCII,  &Text, sizeof(Text));
    but I get the following error from the debugger:

    PHP Code:
    Unhandled exception at 0x66b7ed6a in NEF_Reader.exe0xC0000005:
    Access violation while writing at position 0x00000000
    [php]

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    0x0000000 == NULL pointer. You've got a pointer to a structure; you appear not to have any actual structures themselves. Either you should pass in the address of an existing structure, or your Read function should malloc the space needed for a structure to exist.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    What is the value of count?
    And how is "pointer" declared?

    Code:
    for (int a = 0; a < Count; a++)
    {
         fread(&Text[a], 1, 1, pFile);
        pointer->ASCII[a] = Text[a];
    }
    Is it possible you are using "->" when you should be using "."?

    Jim

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    5,863
    Hmmm ... I wrote too fast in my previous post. It's true that std::string cannot be used in a union, and that strcpy() does not work for unsigned char.

    Check to make sure that pointer is not NULL. If it is, the address of pointer->ASCII[0] will also be NULL (albeit of different type), and writing to a NULL pointer is verboten. Regardless of whether that happens in a loop or with memcpy().
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    @grumpy

    i changed the pointer declaration as followed:
    Code:
    IFDEntry *IFD = new IFDEntry;
    and give it to the function as value:
    Code:
    ReadIFDEntry(272, IFD, File);
    the function is declared as followed:
    Code:
    void ReadIFDEntry(unsigned long Entry, IFDEntry *pointer, FILE *pFile)
    but still I get the same error.

    @jimblumberg

    count has a value of 10
    I'm using VC++ 2008 and if I use . instead of -> the compiler gives me an error:
    PHP Code:
     error C2228left of ".ASCII" has to be a  class/strukture/Union .
    1>        Typ is 'IFDEntry *'
    1>        did you want to use '->' instead

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    ok I found the error! While trying to initialize the value at the pointers adress I used the statement
    Code:
    IFD = 0;
    but that didn't went to the value but the adress itself

    Shame on me -.-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 05:53 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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