面对使用指针的困难

时间:2014-10-22 11:52:00

标签: c++ arrays pointers

我是编程新手,我正在尝试学习c ++,但我在使用指针时面临很多困难。我编写了一个代码,我需要一个动态数组,大小将由用户给出,值也将由用户给出,我想按递增和递减顺序对其进行排序。

我已经完成了静态数组的任务,但有了指针,我面临很多错误,这是我的第一个问题,请关注我。

这是我的代码

#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);
    }
}

以下是错误。

$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);

3 个答案:

答案 0 :(得分:2)

在您的函数原型中:

int *a[]

应该是:

int *a

你想要一个指向int的指针,你实际上为一个指向int的指针数组写了一个声明符。

答案 1 :(得分:2)

其他答案解决了编译器错误,但您的程序在语义上仍然不正确。它表现出不确定的行为。

此处,array_size是一个不确定的值。

int array_size;

IntArrayPtr a;
a = new int[array_size];

不仅使用array_size未定义的行为,而且依赖于此垃圾值,稍后尝试使用用户定义的数字填充数组。您可能打算这样做:

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

顺便说一下,删除array_size = (sizeof(a) / sizeof(*a));,因为您立即丢弃了用户输入并依赖于使用垃圾值初始化的数组的大小。

其次,您的bubblesort不会像您的程序所说的那样按升序排序。您需要将<更改为>

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

在您的sortd功能中,您展示未定义的行为再次,因为您在第一次迭代中访问arr[size]

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

您可以将其更改为j = size - 2,或者意识到除了关系运算符之外,按升序/降序排列的bubblesort是相同的。它变成了:

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])
        {

最后(也是最不重要的)你不会释放你的记忆。请务必在if块中添加delete[] a;


注意:

  • 编译器上的警告可能已经提醒您在初始化之前使用了array_size但是对于我来说它是隐藏的,因为它稍后会在代码中使用。这里的故事的寓意不是写一堆代码,编译它,然后在它不起作用时抱怨。逐步编译代码并一次修复一个问题。
  • 停止尝试重新发明轮子并利用标准库。使用std::sortstd::vector
  • 您可以按降序显示数字,而不是具有几乎相同的两个不同的排序函数。

答案 2 :(得分:0)

您需要更改功能的签名,如下所示:

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

此外,您也可以使用IntArrayPtr a;代替int *a; 并且,把

 int array_size = input;