Thread: can array be assigned a pointer ?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    Unhappy can array be assigned a pointer ?

    Code:
    main(){
          CountingSort c ;
          int inputArray[10] = {23,12,45,46,25,12,32,43,32,29}; 
          int* outputA;
          c.setInputArray(inputArray);
          inputArray = c.sort();
         
         /* for (int i=0;i<10;i++)
              cout<<setw(3)<<inputArray[i]<<endl;*/      
          getchar();
    }
    c.sort() is a function which is some thing like this
    Code:
    int* CountingSort :: sort(void){
         for(int i=0;i<10 ; i++)
             auxilaryArray[inputArray[i]] = auxilaryArray[inputArray[i]] + 1; 
         for (int i=1;i<SIZE ; i++)
             auxilaryArray[i] = auxilaryArray[i] +  auxilaryArray[i-1];
          for (int j=0;j<10;j++)
              outputArray[auxilaryArray[inputArray[j]]] = inputArray[j];
              
          for (int i=0;i<10;i++)
              cout<<setw(3)<<outputArray[i]<<endl; 
         return outputArray;
    }
    it gives me a type incompatible error in types int* to int[10]
    I lost touch in c++ can some one please let me know why. THought that the address can be assigned to array ?.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You may have thought that, but it isn't true. An array is a block of contiguous memory, and for sure you can't move it around. You need to copy values into an array one slot at a time.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    setInputArray(int array[]);  // prototype
    setInputArray(inputArray);  // call
    That's an array pointer, the array is NOT passed by value, so if you modify it inside setInputArray or it's object, it is modified in the source. If you don't plan on doing that, use "const int array[]" and the compiler will warn you if you break your own rule accidently.

    eg:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void test (int ray[]) {
    	int i;
    	for (i = 0; i < 3; i++) cout << ray[i] << endl;
    	ray[1] = 666;
    }
    
    int main(void) {
    	int ray[3] = {1, 2, 3};
    
    	test(ray);
    	cout << ray[1] << endl;
    
    	return 0;
    }
    Last edited by MK27; 06-18-2011 at 09:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You cannot assign anything to an array itself.
    You can only assign stuff to the individual elements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I just thought I'd mention it, but main must return int.
    main with no return type at all is not valid C++ code.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    You cannot assign anything to an array itself.
    Couldn't you overload the assignment operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assignment operators can only be overloaded as member functions of a class type. Since an array is not a class type, it is not possible to overload an assignment operator for it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    Assignment operators can only be overloaded as member functions of a class type. Since an array is not a class type, it is not possible to overload an assignment operator for it.
    Thanks, now you know why I stay on the C board.

    I was never a fan of C++ and its crazy rules, templates, overloading, etc. Plus declaring variables wherever you wanted bugs the crap out of me (but then they added it to C...).


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A lot of those crazy rules in C++ come due to maintaining backward compatibility to C. What goes around comes around, I guess.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding length of characters in an assigned char array ??
    By charlatanksk in forum C Programming
    Replies: 6
    Last Post: 05-04-2008, 02:34 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Replies: 20
    Last Post: 11-12-2005, 03:10 PM
  4. Replies: 1
    Last Post: 05-01-2004, 05:41 AM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM