Thread: assign the value of an array to a pointer c++

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Question assign the value of an array to a pointer c++

    Hello @ all
    I`m trying to do the following:

    Code:
    pTest->tst[0] = user_tst[0];
    content of array is at this index 0x00

    which doesn't work and stucks me to the following error:

    error C2109: subscript requires array or pointer type.

    the array comes from another file and is well defined(there) as it is also made external in the included header file.

    I would be glad, if anyone could give me a hint or point me to correct direction.

    Thanks for every read/help.

    kox
    Last edited by kox; 10-28-2012 at 06:56 PM. Reason: added code tags

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hey kox,
    I would say post a bit more code or the whole file if it is not to big, it is a bit hard to guess only with one line. Don't forget the code tags!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    here you are (code snippets)

    thx for your reply

    here is the relevant code:
    file1 filling the array:

    Code:
    int tst_user[5];
    
    int Fill()
    {
        char variable[] = "";
        char * pch;
        int i = 0;
        
         
        printf("Enter desired Data in this style XX-XX-XX-XX-XX-XX : \n");
        scanf("%s", &variable); //user input to address of variable
        
        pch = strtok (variable, "- :");
        while (pch != NULL)
        {
            printf("%s \n",pch);  //control of splitting
            sscanf (pch, "%x", &tst_user[i]);  //filling
            pch = strtok (NULL, "- :");
            printf("array index %d = %#x \n",i, tst_user[i++]);   //control array and add 1 to index
        }
    
        return 0;
    }
    .
    .
    .
    Fill()
    .
    .
    .
    used header:

    Code:
    extern int tst_user;

    file 2 assigning the array to the pointer:

    Code:
    pTest->tst[0] = tst_user[0]);
    manually(static) assigning works just fine:

    Code:
    pTest->tst[0] = 0x00;
    but this is not what i want.

    The code is very long and the failure should be in the assignment or the interchange of the array itself.

    i also tried it with:
    Code:
    pTest->tst[0] = static_cast<int>(tst_user[0]);
    which gives the same error.

    Thanks for reading/helping

    kox
    Last edited by kox; 10-28-2012 at 06:58 PM. Reason: finer argumentation

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    perhaps show the definition for the class/structure of which pTest is an object.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So what does the definition of tst look like? The error is telling you that it's not an array or pointer, but you're using the square brackets as if it were a pointer.
    You should show the definition of pTest too. It might be that you actually want pTest[0]->tst.
    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"

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    You have
    Code:
    extern int tst_user;
    and
    Code:
    int tst_user[5];
    Hmmm...

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Hi @ all

    @ caligulaminus

    but when i do this :
    Code:
    extern int tst_user[5];
    i get an unresolved externals error;

    i think the definition and the making extern should be correct,
    it compiles... till i use the instance of the array.

    THX

    kox
    Last edited by kox; 10-29-2012 at 08:03 AM.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Hi @ all

    @ iMalc thx for your help

    The pointer is defined correctly, i tried some examples and figured out that the mistake is about the
    Code:
    tst_user[i]
    array.
    For any reason i cant re-use this in the second file.
    It seems that due to the interchange between the two files, the definition gets lost.

    Any ideas

    Thx

    kox
    Last edited by kox; 10-29-2012 at 08:28 AM.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    problem is in sharing the array itself

    Hi @ all

    @ caligalimus
    Sorry, I have to correct myself,
    I think youre right, the correct making external should be with the index([5]).
    the array definition got lost cause i redefined it as an standard integer (without the index).
    So i think the compiler thinks it is an standard variable and therefore the type failure error.

    But in correct defining
    Code:
     extern int tst_user[5];
    there is the problem with the unresolved external, which i should focus on.

    error LNK2001: unresolved external symbol "int * tst_user" (?tst_user@@3PAHA)

    This says nothing to me and English isn't my mother tongue and googeling the prob also doesn't help.

    Could anyone please explain me the error above or give me a hint
    what im doing wrong?

    Thx for reading/helping

    kox

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 02:54 PM
  2. assign value of pointer array
    By zcrself in forum C Programming
    Replies: 12
    Last Post: 08-17-2009, 02:12 PM
  3. Can't Return/Assign Char Pointer
    By SiliconHobo in forum C Programming
    Replies: 21
    Last Post: 12-16-2007, 10:56 AM
  4. Replies: 6
    Last Post: 02-27-2006, 03:11 AM
  5. want to assign myself the adress of a pointer
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 02:19 PM

Tags for this Thread