CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    8

    Post invalid conversion from 'int*' to 'int'

    can you guys help fix this error plz

    Im using myprogramming lab,
    the problem is this:
    The variables arr1 and arr2 have been declared as pointers to integers. An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values. Allocate an array of 20 elements, assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remander of the elements of arr2 to 0.

    here is my code:
    1 main() {
    2 int *arr1, *arr2;
    3
    4 for(int i = 0; i < 20; i++)
    5 {
    6 arr2[i] = new int(i);
    7 if(i < 10)
    8 {
    9 arr2[i] = arr1[i];
    10 }
    11 else
    12 {
    13 arr2[i] = 0;
    14 }
    15 }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: invalid conversion from 'int*' to 'int'

    Quote Originally Posted by mejiac3 View Post
    ... An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values.
    The code snippet you showed does NOT contain this part.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2012
    Posts
    10

    Re: invalid conversion from 'int*' to 'int'

    Quote Originally Posted by mejiac3 View Post
    can you guys help fix this error plz

    Im using myprogramming lab,
    the problem is this:
    The variables arr1 and arr2 have been declared as pointers to integers. An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values. Allocate an array of 20 elements, assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remander of the elements of arr2 to 0.

    here is my code:
    1 main() {
    2 int *arr1, *arr2;
    3
    4 for(int i = 0; i < 20; i++)
    5 {
    6 arr2[i] = new int(i);
    7 if(i < 10)
    8 {
    9 arr2[i] = arr1[i];
    10 }
    11 else
    12 {
    13 arr2[i] = 0;
    14 }
    15 }
    Hi it seems in your code you had conflicting idea ... you declare arr1 and arr2 as interger pointer you should allocated them something like this outside the loop ..

    int *arr1, *arr2;
    Code:
    arr1 = new int[10];
    this means arr1 is a pointer of array of 10 integer, but if you want an array that holds pointer in each elements you should do something like this.
    Code:
    int *arr1[10], *arr2[10];
    this means arr1 and arr2 are arrays of integer pointers

    I tried to modify your code and remove the error

    Code:
    main() {
          int *arr1[10], *arr2[10];
    		 	
         for(int i = 0; i < 20; i++)
        {
    	 arr2[i] = new int(i);
    	 if(i < 10)
    	 {
    		arr2[i] = arr1[i];
    	 }
    	 else
    	{
    	  arr2[i] = 0;
    	}
         }
    }

    I hope it will help

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: invalid conversion from 'int*' to 'int'

    Quote Originally Posted by himitsujanai View Post
    I tried to modify your code and remove the error
    Code:
         int *arr1[10], *arr2[10];
    		 	
         for(int i = 0; i < 20; i++)
        {
    	 arr2[i] = new int(i);
    Do you see a problem here?

    Regards,

    Paul McKenzie

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: invalid conversion from 'int*' to 'int'

    I this case, more than likely you don't really want to be using pointers and new anyway.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured