CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Arrow Redefination problem

    Hi,
    I have a main.c file like this
    Code:
    #include "Headers.h"
    extern char *myArray;
    int main()
    {
        return 0;
    }
    the Headers.h file contains
    char myArray[200];

    When I compile this is giving error like redefination; different types of indirection

    But my doubt is we can use pointers instead of arrays

    please clarify me whether we can use extern with pointer or not

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Redefination problem

    You probably should declare myArray as extern in the header:
    Code:
    extern char myArray[];
    Then in main.c you define it:
    Code:
    char myArray[200];
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: Redefination problem

    Thanks for the quick response.
    I changed the way you told.
    It is working fine.

    But my question is -- I can use pointer also in the main.c file

    like char *myArray

    But it is giving the compilation error -- like redefination; different types of indirection

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Redefination problem

    ---

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Redefination problem

    Quote Originally Posted by Rajesh1978
    But my question is -- I can use pointer also in the main.c file

    like char *myArray

    But it is giving the compilation error -- like redefination; different types of indirection
    I do not know what is your question, but a pointer is not an array, and an array is not a pointer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Redefination problem

    To get a pointer to your array you could do something like :-
    Code:
    char* ptr = &myarray[0];
    Be extremely careful using that pointer though, as by accessiing your array this way, the compiler has no idea of its bounds.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Mar 2010
    Posts
    11

    Re: Redefination problem

    It is not possible to declare variables of the same name twice.
    You have already declared array 'myArray', if you want to declare a pointer to it, name it differently, e.g.
    Code:
    char *myArray_ptr = myArray;
    Next time show the exact error message, line of code where the error is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured