Thread: change pointer of an array

  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 01:37 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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* .

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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];

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 02:10 AM.

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >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

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 08: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, 08:50 AM
  4. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 07:16 AM
  5. change array size
    By dontknow in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2002, 02:25 PM