-1
typedef struct
{
    char name[50];
    int age;
    int sex;
} Person ;

void sortAge(Person x[],int n)
{
    printf("Age sort: \n");
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if (x[i].age > x[j].age)
            {
                int temp = x[i].age; // I change the age
                x[i].age = x[j].age;
                x[j].age = temp;

                temp = x[i].sex; // I change the sex
                x[i].sex = x[j].sex;
                x[j].sex = temp;            

                // how I can use the same to change the names?
                // tried strcpy but no work :/
            }
        }

    }

Using strcpy function

...
char temp2[50];
strcpy(temp2,x[i].name);
etc...

I get this error..

56  27  C:\Users\**\Desktop\Untitled1.cpp   [Error] 'strcpy' was not declared in this scope
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • It would be easier to have a `Person` temp variable, and use `memcpy` to copy the struct, rather than the contents of the struct. – AntonH Mar 29 '14 at 18:05
  • The prototype for `strcpy()` is in ``. Just `#include ` and the error you speak of should disappear. Since you're writing headers, you probably also need to `#include ` for `scanf()` and `printf()` ;) – pmg Mar 29 '14 at 18:05
  • 2
    `Person temp = x[i]; x[i] = x[j]; x[j] = temp;` – BLUEPIXY Mar 29 '14 at 18:09
  • @AntonH it would be even *easier* to just use native structure copying, since the language supports it (See comment from @BLUEPIXY). – WhozCraig Mar 29 '14 at 18:13
  • @WhozCraig I didn't know, which is why I used "memcpy" in my comment. Is it new in C? I don't remember learning about copying it like BLUEPIXY mentions. – AntonH Mar 29 '14 at 18:59
  • @AntonH its been around since at least C89, but I would not be surprised if it has been there since inception. I'd have to review the pre-ANSI specs to know for sure, and unfortunately I don't have them handy. Sry. – WhozCraig Mar 29 '14 at 19:50
  • And a note for the OP: That isn't bubble-sort. Bubble-sort compares *adjacent* elements, rippling up the sequence and swapping as needed until the "top" is reached. at that point the last element is guaranteed to be the "winner" of that sweep. Then "top" is *decreased* by one, and the process is repeated, terminating-outright if no swap happens on any single sweep. The latter is what gives bubble-sort complexity O(N) for an already-sorted sequence.That attribute and its tendency to *heavily* exploit cpu data caching on small sequences are the (only) redeeming qualities of bubble-sort. – WhozCraig Mar 29 '14 at 19:54

1 Answers1

2

error.. 56 27 C:\Users**\Desktop\Untitled1.cpp [Error] 'strcpy' was not declared in this scope

You should include <string.h> in the beginning of your source file.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47