Home > database >  I'm a newbie in C ,and I 'm now stuck in pointer
I'm a newbie in C ,and I 'm now stuck in pointer

Time:03-02

I create a function to display the element of an array with position shift to right 3,In case the element overload if will shift to the left, I used pointer to pass by value.The code almost worked but it display 0 instead of input elements. Can somebody show me why, pls!

#include <iostream>
#include <cmath>
using namespace std;
int * sort (const int * const ,int );
void display_origin_order(const int * const ,int );
int * sort(const int * const array,int size)
{
    for (int i;i<size;  i)
     {
         if(i 3<size||i 3==size)
         {
          cout<<*(array i 3)<<" ";
         }
         if (i 3>size)
         {
          cout<<*(array i-2)<<" ";
         }
     }
     return 0;
    cout<<endl;
}
void display_origin_order(const int * const array,int size)
{
    for (int i;i<size;  i)
     {
      cout<<array[i]<<" ";
     }
    cout<<endl;
}
int main ()
{
    int * first_array{nullptr};
    int size{0};
    first_array = new int [size];
    int init_value{0};
    cout<<"Please enter the size of the array \n";
    cin>>size;
    cout<<"Now its elements \n";
    for (int i;i<size;  i)
    {
        cout<<"input variable \n";
        cin>>init_value;
        first_array[i] = init_value;
    }
    cout<<"The array's elements in origin order are : ";
    display_origin_order(first_array,size);
    cout<<"The array's elements in required order are: ";
    sort (first_array,size);
    delete [] first_array;
    return 0;
}

CodePudding user response:

Actually, your bound check is actually out of bound. The array index starts from 0, and ends in size-1, and if you want to shift the element, you should substract size, not 2. In addition, you did not initialized i in the for loops in your program, which leads to undefined behavior. Here's my modification:

int * sort(const int * const array,int size)
{
    for (int i=0;i<size;  i)
     {
         if(i 3<size)
         {
          cout<<*(array i 3)<<" ";
         }
         else
         {
          cout<<*(array i 3-size)<<" ";
         }
     }
     return 0;
    cout<<endl;
}

Addition : sort is not a proper name for this function. I suggest the name display_shift to reduce the reader's confusion.

EDIT: As @user17732522 pointed out, main function also has some bugs. you initialized the first_array by new int[size], when the size is 0. you have to initialize the first_array after getting the size like this:

int main ()
{
    int * first_array{nullptr};
    int size{0};
    int init_value{0};
    cout<<"Please enter the size of the array \n";
    cin>>size;
    first_array = new int [size];        // initialize the first_array AFTER you get its size...
    cout<<"Now its elements \n";
    for (int i=0;i<size;  i) // You forgot the initialization here, too!
    {
    // rest is same...
    }
}

Addition 2 : When you trying to shift the elements, there is more simpler, safer way to do this, using the operator %:

void display_shift(const int * const array,int size)
{
    for (int i=0;i<size;  i)
    {
        cout<<*(array (i 3)%size)<<" ";
    }
}

That's because when the size is so small that i 3 is more than twice the size, you actually have subtract 2*size, and the program will be more complicated. modulo operator(%) reduces those complication, and you do not have to check bound when using this operator!

CodePudding user response:

The reason you are not prompting for input is because you do not initialize i in your for loops. Your for loops currently are for (int i; i < size; i), but should be for (int i = 0; i < size; i). If you do not initialize i, then its value is undefined, and it may start with any value which causes the loop to happen any number of times (or no times at all).

  •  Tags:  
  • c
  • Related