Help with C errors C2440 and C2664.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnsonbl
    New Member
    • Jan 2008
    • 4

    Help with C errors C2440 and C2664.

    pttempmonthaddr = &tempmonth;
    pttempdayaddr = &tempday;

    separate(date, pttempmonthaddr , pttempdayaddr);/*function call*/
    printf("\nThe month entered is %s and day entered is %f.\n", pttempmonthaddr , *pttempdayaddr) ;
    return 0;

    error C2440: '=' : cannot convert from 'char (*__w64 )[81]' to 'char *'

    error C2664: 'separate' : cannot convert parameter 3 from 'int *' to 'int'
    There is no context in which this conversion is possible


    I am getting the C2440 error in the top line and C2664 error in the 3rd line from top. Can somebody help me out as to why?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Probably your array definition has something top do with the C2440.

    The C2664 is caused by a mismatch in the type of the third argument versus what you supplied. It looks like the third argument needs to be an int and you supplied an int*

    I can't tell since you didn't post the definitions of your variables or your function prototype.

    You mioght read this on arrays:
    Originally posted by weaknessforcats
    First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
    [code=c]
    int array[5];
    [/code]

    That is an array of 5 elements each of which is an int.

    [code=c]
    int array[];
    [/code]

    won't compile. You need to declare the number of elements.

    Second, this array:
    [code=c]
    int array[5][10];
    [/code]

    is still an array of 5 elements. Each element is an array of 10 int.

    [code=c]
    int array[5][10][15];
    [/code]

    is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


    [code=c]
    int array[][10];
    [/code]

    won't compile. You need to declare the number of elements.

    Third, the name of an array is the address of element 0
    [code=c]
    int array[5];
    [/code]

    Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

    [code=c]
    int array[5][10];
    [/code]

    Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
    [code=c]
    int array[5][10];

    int (*ptr)[10] = array;
    [/code]

    Fourth, when the number of elements is not known at compile time, you create the array dynamically:

    [code=c]
    int* array = new int[value];
    int (*ptr)[10] = new int[value][10];
    int (*ptr)[10][15] = new int[value][10][15];
    [/code]

    In each case value is the number of elements. Any other brackets only describe the elements.

    Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

    [code=c]
    int** ptr = new int[value][10]; //ERROR
    [/code]

    new returns the address of an array of 10 int and that isn't the same as an int**.

    Likewise:
    [code=c]
    int*** ptr = new int[value][10][15]; //ERROR
    [/code]

    new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

    With the above in mind this array:
    [code=cpp]
    int array[10] = {0,1,2,3,4,5,6, 7,8,9};
    [/code]
    has a memory layout of

    0 1 2 3 4 5 6 7 8 9

    Wheras this array:
    [code=cpp]
    int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
    [/code]
    has a memory layout of

    0 1 2 3 4 5 6 7 8 9

    Kinda the same, right?

    So if your disc file contains

    0 1 2 3 4 5 6 7 8 9

    Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

    Therefore, when you do your read use the address of array[0][0] and read as though you have a
    one-dimensional array and the values will be in the correct locations.

    Comment

    • johnsonbl
      New Member
      • Jan 2008
      • 4

      #3
      Originally posted by weaknessforcats
      Probably your array definition has something top do with the C2440.

      The C2664 is caused by a mismatch in the type of the third argument versus what you supplied. It looks like the third argument needs to be an int and you supplied an int*

      I can't tell since you didn't post the definitions of your variables or your function prototype.

      You mioght read this on arrays:

      Thanks for the reply. I am still working on it. Here is the entire code.

      #include<stdio. h>
      #include<string .h>
      #include<stdlib .h>
      #define MSIZE 81
      void separate (char[], char[], int);/*function prototype*/
      int main()
      {
      char date [MSIZE];
      char tempmonth[MSIZE];
      int tempday;
      printf("\nEnter a month and a day. (Ex. June 14)\n");
      gets_s(date);
      printf("You entered: %s\n", date);

      char *pttempmonthadd r;
      int *pttempdayaddr;

      pttempmonthaddr = &tempmonth;
      pttempdayaddr = &tempday;

      separate(date, pttempmonthaddr , pttempdayaddr);/*function call*/
      printf("\nThe month entered is %s and day entered is %f.\n", *pttempmonthadd r, *pttempdayaddr) ;
      return 0;
      }
      void separate(char *strin, char *strout, int *iout) /*function header*/
      {
      iout = 0;

      while(*strin != '/0' && *strin != ' ')
      *strout++ = *strin++;
      *strout = '\0';
      while (*strin != '\0' && *strin != ' ')
      *strin++;

      if(*strin != '\0')
      *iout = atoi(strin);
      }

      Comment

      Working...