problem with double array assignment with a single type

This is a discussion on problem with double array assignment with a single type within the C Programming forums, part of the General Programming Boards category; i have two structures declared as such: Code: struct rtpkt { int src; /* source router */ int dst; /* ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    problem with double array assignment with a single type

    i have two structures declared as such:
    Code:
    struct rtpkt {
    	int src;          /* source router                                   */
    	int dst;          /* destination router (immediate neighbor)         */
    	int mincost[N];   /* minimun cost to nodes 0, 1, 2, and 3            */
    };
    
    struct dt
    {
      int dist[N][N];     /* dist[i][j] is the distance to node i via node j */
    };
    And i have a line in the .c file:
    Code:
                struct rtpkt send_neighbor;
                send_neighbor.mincost = dt[node].dist[node];
    and the error that im getting is (cant assign two different types) not a quote.
    What am i doing wrong here?

    -thank you
    Last edited by armen; 11-17-2008 at 01:34 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The left side is an array of ints, the right side is one int. Perhaps you want to put the number into a particular slot in your array?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    The left side is an array of ints, the right side is one int. Perhaps you want to put the number into a particular slot in your array?
    how is the right side one int? in my understanding, (apparently a wrong one)
    doublearray[10][10] is an array of double pointers, so doublearray[1] is pointing to a location in memory that is the start of a 10*sizeof(int) block.
    so is onearray[10], which onearray is pointing to the same sort of structure.

    what is wrong from my understanding?
    Last edited by armen; 11-17-2008 at 01:43 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We don't have any doublearray or onearray anywhere here. I didn't quote your original, but now what you have is an array and an array. However, arrays are not assignable (in C or C++). If you want each element of the first array to have the corresponding value in the second array, you'll need to use a for-loop to make it happen.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    memcpy works in C...
    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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elysia View Post
    memcpy works in C...
    That would require him to have the same data types on both sides, which I'm not sure he does, what with this "double" stuff appearing out of nowhere.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Hrm, well, let's leave that subject untouched until more information appears.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Quote Originally Posted by tabstop View Post
    We don't have any doublearray or onearray anywhere here. I didn't quote your original, but now what you have is an array and an array. However, arrays are not assignable (in C or C++). If you want each element of the first array to have the corresponding value in the second array, you'll need to use a for-loop to make it happen.
    I think i confused you with 2D array when i said double.
    so considering the variable
    int dist[N][N]
    and
    int mincost[N]
    and can't have mincost to point to dist[N], by setting mincost = dist[N] ?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    No, you cannot. Use memcpy.
    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.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Or use a more obvious for loop to copy each member of the array.

    Why?
    1. It's the more natural thing to do.
    2. Chances are, you'll screw up the 'size' parameter of memcpy at some point, and either copy too little, or too much data.
    3. It might actually be quicker at run-time, depending on your implementation of memcpy.
    4. When you decide to use C++, then the assignment approach will seem natural, and applying the technique to complex objects (with an overloaded assignment operator) will continue to "do the right thing". memcpy in this instance would be a disaster.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Salem View Post
    3. It might actually be quicker at run-time, depending on your implementation of memcpy.
    Normally I would agree, but I don't think I can do that with this one...
    memcpy is an optimized and fast function for copying memory, so it will probably be faster, if anything. Or should be.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by armen View Post
    I think i confused you with 2D array when i said double.
    so considering the variable
    int dist[N][N]
    and
    int mincost[N]
    and can't have mincost to point to dist[N], by setting mincost = dist[N] ?
    No you cannot do that because the name of an array mincost is not a variable and as Salem suggested use a loop after an assignment like
    Code:
    int *ip = mincost

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Your code seems misleading, since you are actually creating a new pointer and assigning to it instead of assigning to the destination array.
    Although it does support assigning a structure to a structure, which pretty much means it is possible to assign a range of values to another range of values, just in a different form. You may think it is strange, but it's just how it is.
    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.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    I see how it can be misconstrued; ofcourse the OP can simply use a pointer and an offset in a loop for array assignment as in
    Code:
    *(mincost + i) = *(dist[0] + i);

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Normally I would agree, but I don't think I can do that with this one...
    memcpy is an optimized and fast function for copying memory, so it will probably be faster, if anything. Or should be.
    But it's probably better at copying 400-40000 bytes than 40 bytes. After all, the compiler may well come up with roughly the same loop as a short memcpy. And there is the overhead of the call or stuffing the right values in the right registers for a rep movs sequence (assuming x86). So I'm with Salem here. Use a loop.

    Obviously, if we have 1000 indices at each size, memcpy() will probably be a tad faster.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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