Results 1 to 6 of 6

Thread: How to get an 'instant copy' of an array returned by a function used two times ....

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get an 'instant copy' of an array returned by a function used two times ....

    The title space is not enough to explain my problem.

    I have a my_function at class A:
    Qt Code:
    1. void my_function(char array1[],char array2[]);
    To copy to clipboard, switch view to plain text mode 

    I call it so:
    my_function(classb->give_me_array(22), classb->give_me_array(33));

    'Give_me_array' located at another class B, receive an index and fill the internal array with 5 values.

    Qt Code:
    1. char * give_me_array(int index) {
    2. fill internal_array....
    3. return internal_char_array;}
    To copy to clipboard, switch view to plain text mode 

    internal_char_array is a char array[5] declared as private for this class

    Ok, my problem is that my_function receives the value for 33 also for 22. ( I think that this is a pointer problem) . How to solve this ?
    I'd not want to use two aditional arrays and function calls to solve the problem.
    Any idea ?

    I'd need a trick to make an instant copy ....

    Thanks

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get an 'instant copy' of an array returned by a function used two times ..

    Qt Code:
    1. void give_me_array(int index, char* outputArray) {
    2. fill internal_array....
    3. strcpy(outputArray, internal_char_array);
    4. }
    To copy to clipboard, switch view to plain text mode 

    When you call give_me_array, make sure "outputArray" has enough space (i.e. greater than 5 bytes if your internal_char_array is always char array[5)

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get an 'instant copy' of an array returned by a function used two times ..

    Ok, but to do this I need an external outputArray ....
    As I know I can call this function two times in the same line, a trick could be having two internal arrays to return the internal_array1 or internalarray2 ... ?
    What do you think ?

  4. #4
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get an 'instant copy' of an array returned by a function used two times ..

    Well, what can I tell you, your code design is wrong.

    Why you want to return a pointer that points to your private class member ? Any user of your class may change your private member without your knowledge.
    In the best case I would return a const pointer, but this is also a bad scenario in some situations.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get an 'instant copy' of an array returned by a function used two times ..

    Why don't you just use QList or QVector ? It will save you a lot of coding.

  6. #6
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get an 'instant copy' of an array returned by a function used two times ..

    Thanks to everybody and for the ideas

Similar Threads

  1. Replies: 1
    Last Post: 4th August 2010, 17:13
  2. Using array with QAxWidget::dynamicCall() function
    By tom0485 in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2010, 10:31
  3. how to copy QString content into array
    By eric in forum Qt Programming
    Replies: 12
    Last Post: 14th November 2007, 23:13
  4. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.