0
#include<iostream>
#include<string.h>
#include<conio.h>

using namespace std;

int main()
{
    char name[30][50];
    char aux[50];
    int n,ok;

    cout<<"number of pupils : ";
    cin>>n; 

    for(int i=0;i<n;i++)
    {
        cout<<"name : ";
        cin>>name[i];
    }

    do
    {
        ok=0;

        for(int i=0;i<n-1;i++)
        {
            if(strcmp(name[i],name[i+1])>0)
            {
                aux=name[i+1];   // here is the bug
                name[i+1]=name[i];
                name[i]=aux;
                ok=1;
            }
        }
    }while(ok==1);

    for(int i=0;i<n;i++)
    {
        cout<<name[i]<<endl;
    }
}

I know it is a noob question, but I am grateful for your help. How can I assign a char[][] to a char[] ? this error occurs 3 times in my code and I do not have enough knowledge to figure it out why

1 Answers1

1

Use char* aux; instead.

When we write char aux[50];, we are immediately allocating memory for ~50 chars. Here you're only swapping pointers to the char arrays (elements in the array of char array, i.e. 2D char array called name), so a char* is what you're looking for.

If you are okay with using the standard library, use std::swap like so:

std::swap(name[i], name[i + 1]);
  • @RaresMatei My pleasure! Please accept the answer that best suits what you’re asking. –  Nov 27 '17 at 20:33