New Coder
New Coder

Reputation: 23

Facing difficulty in Using pointers

I am very new to programming and i am trying to learn c++ but i am facing a lot of difficulties in using pointers. I have written a code where i need a dynamic array, the size will be given by the user and the values will be given by the user too, and i want to sort it in increasing and decreasing order.

I have done the task with static arrays but with pointers i am facing a lot of errors, this is my first question please be light on me.

here is my code

#include <iostream>
#include <cstdlib>
#include <cstddef>
using namespace std;

typedef int *IntArrayPtr;

void fill_array(int *a[], int size)
{
    cout << "Enter" << size << "integers.\n";
    for (int index = 0; index < size; index++)
    {
        cin >> a[index];
    }
}
void sort(int *a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = 0; j < size - 1; j++)
        {
            if (a[j] < a[j + 1])
            {
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }

    }
    cout << "in ascending order the numbers are:";
    for (int index = 0; index < size; index++)
    {
        cout << a[index] << " ";
        cout << endl;
    }
}
void sortd(int *arr[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = size - 1; j >= 0; j--)
        {
            if (arr[j + 1] > arr[j])
            {
                int temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    cout << "in descending order the numbers are:";
    for (int index = 0; index < size; index++)
    {
        cout << arr[index] << " ";
        cout << endl;
    }
}
int main()
{
    int input;
    cout << "to sort via forward sorting press 1, for backward sorting press 2";
    cin >> input;
    int array_size;

    IntArrayPtr a;
    a = new int[array_size];


    if (input == 1)
    {
        //int array_size;
        cout << "how many numbers will be sorted?";
        cin >> array_size;
        array_size = (sizeof(a) / sizeof(*a));
        fill_array(a, array_size);
        sort(a, array_size);
    }
    else if (input == 2)

    {
        int array_size1;
        cout << "how many numbers will be sorted?";
        cin >> array_size1;
        array_size1 = (sizeof(a) / sizeof(*a));
        fill_array(a, array_size1);
        sortd(a, array_size1);
    }
}

Following are the errors.

$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function �void fill_array(int**, int)�:
main.cpp:13:12: error: no match for �operator>>� (operand types are �std::istream {aka std::basic_istream<char>}� and �int*�)
         cin>> a[index];
            ^
main.cpp:13:12: note: candidates are:
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:120:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(__istream_type& (*__pf)(__istream_type&))
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:120:7: note:   no known conversion for argument 1 from �int*� to �std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:124:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
       operator>>(__ios_type& (*__pf)(__ios_type&))
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:124:7: note:   no known conversion for argument 1 from �int*� to �std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:131:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(ios_base& (*__pf)(ios_base&))
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:131:7: note:   no known conversion for argument 1 from �int*� to �std::ios_base& (*)(std::ios_base&)�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:168:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(bool& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:168:7: note:   no known conversion for argument 1 from �int*� to �bool&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:172:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]
       operator>>(short& __n);
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:172:7: note:   no known conversion for argument 1 from �int*� to �short int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:175:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(unsigned short& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:175:7: note:   no known conversion for argument 1 from �int*� to �short unsigned int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:179:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]
       operator>>(int& __n);
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:179:7: note:   no known conversion for argument 1 from �int*� to �int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:182:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(unsigned int& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:182:7: note:   no known conversion for argument 1 from �int*� to �unsigned int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:186:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(long& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:186:7: note:   no known conversion for argument 1 from �int*� to �long int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:190:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(unsigned long& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:190:7: note:   no known conversion for argument 1 from �int*� to �long unsigned int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:195:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(long long& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:195:7: note:   no known conversion for argument 1 from �int*� to �long long int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:199:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(unsigned long long& __n)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:199:7: note:   no known conversion for argument 1 from �int*� to �long long unsigned int&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:214:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(float& __f)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:214:7: note:   no known conversion for argument 1 from �int*� to �float&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:218:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(double& __f)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:218:7: note:   no known conversion for argument 1 from �int*� to �double&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:222:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(long double& __f)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:222:7: note:   no known conversion for argument 1 from �int*� to �long double&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:235:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
       operator>>(void*& __p)
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:235:7: note:   no known conversion for argument 1 from �int*� to �void*&�
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:259:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
       operator>>(__streambuf_type* __sb);
       ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:259:7: note:   no known conversion for argument 1 from �int*� to �std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}�
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:53:0,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/locale_classes.h:40,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/ios_base.h:41,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/ios:42,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/ostream:38,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:39,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
     operator>>(basic_istream<_CharT, _Traits>& __in,
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.tcc:996:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   mismatched types �std::basic_string<_CharT, _Traits, _Alloc>� and �int*�
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:955:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   deduced conflicting types for parameter �_CharT2� (�char� and �int�)
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/istream:879:0,
                 from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/istream.tcc:923:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   deduced conflicting types for parameter �_CharT� (�char� and �int*�)
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:727:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   cannot convert �*(a + ((sizetype)(((long unsigned int)index) * 8ul)))� (type �int*�) to type �unsigned char&�
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
     operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:732:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   cannot convert �*(a + ((sizetype)(((long unsigned int)index) * 8ul)))� (type �int*�) to type �signed char&�
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
     operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:774:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   cannot convert �*(a + ((sizetype)(((long unsigned int)index) * 8ul)))� (type �int*�) to type �unsigned char*�
         cin>> a[index];
                      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:40:0,
                 from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
     operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
     ^
/usr/local/gcc-4.8.1/include/c++/4.8.1/istream:779:5: note:   template argument deduction/substitution failed:
main.cpp:13:22: note:   cannot convert �*(a + ((sizetype)(((long unsigned int)index) * 8ul)))� (type �int*�) to type �signed char*�
         cin>> a[index];
                      ^
main.cpp: In function �void sort(int**, int)�:
main.cpp:24:31: error: invalid conversion from �int*� to �int� [-fpermissive]
                 int temp = a[j];
                               ^
main.cpp:26:24: error: invalid conversion from �int� to �int*� [-fpermissive]
                 a[j+1] = temp;
                        ^
main.cpp: In function �void sortd(int**, int)�:
main.cpp:45:35: error: invalid conversion from �int*� to �int� [-fpermissive]
                 int temp = arr[j+1];
                                   ^
main.cpp:47:24: error: invalid conversion from �int� to �int*� [-fpermissive]
                 arr[j] = temp;
                        ^
main.cpp: In function �int main()�:
main.cpp:74:57: error: cannot convert �IntArrayPtr {aka int*}� to �int**� for argument �1� to �void fill_array(int**, int)�
                                 fill_array(a, array_size);
                                                         ^
main.cpp:75:51: error: cannot convert �IntArrayPtr {aka int*}� to �int**� for argument �1� to �void sort(int**, int)�
                                 sort(a, array_size);
                                                   ^
main.cpp:84:50: error: cannot convert �IntArrayPtr {aka int*}� to �int**� for argument �1� to �void fill_array(int**, int)�
                         fill_array(a, array_size1);
                                                  ^
main.cpp:85:45: error: cannot convert �IntArrayPtr {aka int*}� to �int**� for argument �1� to �void sortd(int**, int)�
                         sortd(a, array_size1);

Upvotes: 0

Views: 89

Answers (3)

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

You need to change the signature of the functions as below:

void fill_array(int* a, int size) {
...
}
void sort(int* a, int size) {
...
}
void sortd(int* arr, int size) {
...
}

Also, instead of IntArrayPtr a; you may use int *a; as well And, put

 int array_size = input;

Upvotes: 0

user3920237
user3920237

Reputation:

The other answers addressed the compiler error but your program is still semantically incorrect. It exhibits undefined behavior.

Here, array_size is an indeterminate value.

int array_size;

IntArrayPtr a;
a = new int[array_size];

Not only is using array_size undefined behavior, but you rely on this garbage value and later try to populate your array with a user-defined number. You probably intended to do this instead:

cout << "how many numbers will be sorted?";
cin >> array_size;
a = new int[array_size];

By the way, remove array_size = (sizeof(a) / sizeof(*a)); because you're immediately discarding the user input and relying on the size of an array that was initialized with a garbage value.

Secondly, your bubblesort does not sort in ascending order like your program says. You need to change < to >:

for (int j = 0; j < size - i - 1; j++)
{
     if (a[j] > a[j + 1])
     {

In your sortd function, you exhibit undefined behavior again because you're accessing arr[size] in the first iteration:

    for (int j = size - 1; j >= 0; j--)
    {
        if (arr[j + 1] > arr[j])
        // j + 1 == size

You could change it to j = size - 2, or realize that a bubblesort in ascending/descending order are identical except for the relational operator. It becomes:

for (int i = 0; i < size - 1; i++)
{
    for (int j = 0; j < size - i - 1; j++)
    {
        // Note the less-than operator
        if (arr[j] < arr[j + 1])
        {

Finally (and least important) you don't free your memory. Remember to add delete[] a; within your if blocks.


Notes:

  • Warnings on your compiler may have alerted you to array_size being used before it was initialized but for me it was hidden because of its usage later in the code. The moral of the story here is not to write a bunch of code, compile it, and then complain when it doesn't work. Compile your code incrementally and fix your issues one at a time.
  • Stop trying to reinvent the wheel and take advantage of the standard library. Use std::sort and std::vector.
  • You could have displayed the numbers in descending order rather than having two different sort functions that are virtually identical.

Upvotes: 2

M.M
M.M

Reputation: 141554

In your function prototypes:

int *a[]

should be:

int *a

You want a pointer to int , you actually wrote a declarator for an array of pointers to int.

Upvotes: 2

Related Questions