Thread: program

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    program

    1)To implement a program which creates a dynamic two-dimensional table of integers (n, k). The parameters n, k will be given by the user via the command line in the main program. The program will stop execution when the user does not introduce the dimensions of the table at the command line.
    2)Then using a pointer byte to set all the elements of the table at the price 65280,form is 00 00 FF 00. For initialization of the table values ​​should used only the pointer, not allowed to make direct recourse to data in the table. At the end of the program printed the contents of the table.

    any idea to solve the second one?
    Last edited by paixtouras; 01-21-2017 at 08:36 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 2)Then using a pointer byte to set all the elements of the table at the price 65280,form is 00 00 FF 00
    You mean you have something like
    Code:
    int **arr;
    // some calls to malloc to create dynamic two-dimensional table of integers (n, k).
    char *ptr = arr;  // huh?
    This doesn't make a lot of sense.
    Sure, you can do it, but you need to take into account a couple of things.
    1. what the size of an integer is - in bytes; this is easy, it's just sizeof(int)
    2. what the endianess of your machine is. This is a lot trickier to solve in any general way.

    You see, if you just assume that int's are 4 bytes, then the endian issue means you need to either write "00 00 FF 00" or "00 FF 00 00"

    If you're allowed to use one more temporary int, then life is a lot easier on the 2nd point. You can simply declare
    int pattern = 65280;

    Then filling the array one byte at a time consists purely of
    memcpy(ptr,&pattern,sizeof(pattern));
    in a loop which increments ptr in an appropriate manner.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  3. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  4. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  5. Replies: 1
    Last Post: 03-03-2009, 04:47 PM

Tags for this Thread