Like Tree2Likes
  • 1 Post By manasij7479
  • 1 Post By laserlight

change pointer of an array

This is a discussion on change pointer of an array within the C++ Programming forums, part of the General Programming Boards category; I have two arrays. char array1[64]="Hello World" //left out bytes are padded with NULL char array2[64]="Nothing to see here"//left out ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    change pointer of an array

    I have two arrays.
    char array1[64]="Hello World" //left out bytes are padded with NULL
    char array2[64]="Nothing to see here"//left out bytes are padded with NULL

    Now I want to copy all the contents of array1 to array2 but I don't want to copy the data itself. How can I change the pointer of Array2?

    If there is not a convenient way to do this, please tell me about an pointer array. How to initialise it and all?
    Last edited by Swoorup; 11-27-2011 at 12:37 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Swoorup View Post
    Now I want to copy all the contents of array1 to array2 but I don't want to copy the data itself. How can I change the pointer of Array2?
    That would waste the space originally used by array2..but a simple assignment should work.
    It would be the same if array2 was declared as a char* .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Thank you! I just got to think about it. But why is this not working
    char *Array1[64]=malloc(sizeof(char)*64);


    This is the error I am encountering
    1 IntelliSense: initialization with '{...}' expected for aggregate object c:\users\sueren\documents\visual studio 2010\projects\ecs project\dynamicadj\main.cpp 25 34 K2Master

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by Swoorup
    Now I want to copy all the contents of array1 to array2 but I don't want to copy the data itself.
    That is a contradiction since copying the contents of array1 to array2 means copying the data itself.

    Quote Originally Posted by Swoorup
    How can I change the pointer of Array2?
    You have two arrays. I don't see any pointers.

    Quote Originally Posted by Swoorup
    If there is not a convenient way to do this, please tell me about an pointer array. How to initialise it and all?
    Perhaps what you want to do is to make array2 a pointer instead (and hence rename it), and then simply get this pointer to point to the first element of array1.

    Quote Originally Posted by manasij7479
    a simple assignment should work
    No, assignment to an array is not allowed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Swoorup View Post
    Thank you! I just got to think about it. But why is this not working
    char *Array1[64]=malloc(sizeof(char)*64);
    Remove the [64] here...and for C++ you'd need a cast.

    It is better to use new anyway..
    Code:
    char* array1 = new char[64];
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by laserlight View Post
    No, assignment to an array is not allowed.
    Hmm... I didn't know that.
    Just thought it was..because I had done it with pointers.
    Thanks for the correction... err.. Your Highness ! :P
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Quote Originally Posted by manasij7479 View Post
    Remove the [64] here...and for C++ you'd need a cast.

    It is better to use new anyway..
    Code:
    char* array1 = new char[64];
    Huh, then how will I transverse through the elements of the array?

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Swoorup View Post
    Huh, then how will I transverse through the elements of the array?
    By a loop...
    *(array1+n) <--Vary n from 0 to 63 to get the elements.
    (I think indices are allowed too..but I'm not very fond of using them with pointers)
    Last edited by manasij7479; 11-27-2011 at 01:10 AM.
    Swoorup likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    I am sorry but this is getting me a bit confused. Suppose I want to access *array2[4] then what do I have to write.
    And will array[0] contain the address to the first element?

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >Suppose I want to access *array2[4]
    Do you have an array of pointers ..or an array of characters ? ...It array2[4] isn't a pointer..the code is faulty.

    >And will array[0] contain the address to the first element?
    Yes
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by manasij7479
    By a loop...
    *(array1+n) <--Vary n from 0 to 63 to get the elements.
    (I think indices are allowed too..but I'm not very fond of using them with pointers)
    I am not very fond of writing *(array1 + n) when array1[n] is clearer. If you want to loop by incrementing an array index, use index notation.

    Quote Originally Posted by Swoorup
    Suppose I want to access *array2[4] then what do I have to write.
    You would write *array2[4], of course

    I suggest that you post an updated code snippet, and tell us (in words) what exactly you want to access.
    Swoorup likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Okay here is what I have
    Code:
    char Array1[64]="Hello World";
    
    /* Now how to make another pointer array which holds the data of Array1*/

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Here is an example:
    Code:
    char Array1[64] = "Hello World";
    char* p = Array1;
    std::cout << p[0] << std::endl;
    Note that p is not a "pointer array". p is a pointer to char. It is not that p "holds the data of Array1", but that p points to the first element of Array1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Wooty!!! thanks a lot.

    Thank you guys!

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Why not use std::string or std::vector or std::array?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change array length
    By miroslavgojic in forum C Programming
    Replies: 6
    Last Post: 11-28-2010, 07:56 AM
  2. How do you change the size of a global array?
    By kbfirebreather in forum C Programming
    Replies: 10
    Last Post: 10-17-2009, 08:39 PM
  3. Replies: 6
    Last Post: 11-29-2004, 07:50 AM
  4. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 06:16 AM
  5. change array size
    By dontknow in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2002, 01:25 PM

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