passing a string from one variable to another

This is a discussion on passing a string from one variable to another within the C Programming forums, part of the General Programming Boards category; hello, im trying to write a program that involves me needing to be able to arrange a bunch of names ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    passing a string from one variable to another

    hello, im trying to write a program that involves me needing to be able to arrange a bunch of names in a specific order that have been scanned from a file.

    the exact order is determined by a function i wrote that compares a bunch of numerical values as show:

    Code:
    void sort(double ppg[999], int numPlayers) {
      int y, x;
                                                   // These keep it within range of datafile
        for (x = numPlayers - 1; x > 0; x = x - 1)
          for (y = 0; y < x; y = y + 1)
            if (ppg[y] < ppg[y+1])          // Compare size of integers
              swap(&ppg[y], &ppg[y+1]);
      }
    
    void swap(double *a, double *b) {
    
      double temp;
      temp = *a;
      *a = *b;
      *b = temp;
    
    }
    this code just compares two numbers and swaps them based on which is greater. however, a lot of the data i have to swap is strings of first names, last names etc..

    so i was wondering if anyone knows how i could transfer the string in one variable into a second variable (to basically get the same effect as switching numbers)

    i tried this:

    Code:
    void charswap(char *c[20], char *d[20]) {
    
      char temp[20];
      temp = *c;
      *c = *d;
      *d = temp;
    
    }
    but i got an error and i dont think strings can simply be set to equal a different variable to swap them the same way as a number would.

    so does anyone know how to pass a string from one variable to another?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    void charswap(char c[20], char d[20])
    {
      char temp[20];
      strcpy(temp, c);
      strcpy(c, d);
      strcpy(d, temp);
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    hmmm, it makes a lot of sense. i updated it to:

    Code:
    void swap(double *a, double *b) {
    
      double temp;
      temp = *a;
      *a = *b;
      *b = temp;
    
    }
    
    void charswap(char c[20], char d[20])
    {
      char temp[20];
      strcpy(temp, c);
      strcpy(c, d);
      strcpy(d, temp);
    }
    
    //-------------------------------------------------------------
    // Function that will compare which variable is larger than the
    // others and swap if true
    //-------------------------------------------------------------
    
    void sort(double ppg[999], int numPlayers, int games_played[999], int goals[999], int assissts[999],
      int points[999], char first_names[999], char last_names[999], char team_name[999], char position [999]) {
      int y, x;
                                                   // These keep it within range of datafile
        for (x = numPlayers - 1; x > 0; x = x - 1)
          for (y = 0; y < x; y = y + 1)
            if (ppg[y] < ppg[y+1])          // Compare size of integers
              swap(&ppg[y], &ppg[y+1]);
              swap(&games_played[y], &games_played[y+1]);
              swap(&goals[y], &goals[y+1]);
              swap(&assissts[y], &assissts[y+1]);
              swap(&points[y], &points[y+1]);
              charswap(first_names[y], first_names[y+1]);
              charswap(last_names[y], last_names[y+1]);
              charswap(team_name[y], team_name[y+1]);
              charswap(position[y], position[y+1]);
      }
    and then i go to call on it with this:

    Code:
     sort(ppg, numPlayers, points, goals, assissts, games_played, first_names, last_names, team_name, position);
    but i get about 12 different error messages saying

    passing argument 1 of charswap makes poitner from integer without a cast

    and

    passing argument 7/8/9 of sort from incompatible pointer type

    i tried playing around with the functions like adding pointers to the charswap function but it didnt change anything

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void charswap(char *c, char *d)
    
    
    
    charswap(&first_names[y], &first_names[y+1]);
    Last edited by CommonTater; 04-02-2011 at 09:48 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your code had an index array in it, why not sort with that?

    Code:
    /*
    Daniel Sedin VAN L 73 38
    Steven Stamkos TBL C 72 43
    Henrik Sedin VAN C 73 18
    Martin StLouis TBL R 72 26
    Corey Perry ANA R 72 39
    Alex Ovechkin WSH L 73 29
    Henrik Zetterberg DET L 73 21
    Jonathan Toews CHI C 70 30
    Anze Kopitar LAK C 73 25
    Jarome Iginla CGY R 75 33
    Patrick Sharp CHI L 71 34
    Brad Richards DAL C 62 25
    Teemu Selanne ANA R 63 22
    Sidney Crosby PIT C 41 32
    
    
    */
    
    
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 14
    
    void sort(int index[MAX], char fname[][22], int key);
    
    int main(void) {
    
      int i = 0, j, got, len, index[MAX];
      char fname[MAX][22];
      char lname[MAX][22];
      char team[MAX][4];
      char plays[MAX];
      int  data[MAX][2];
      
    
      FILE *fp;
    
      printf("\n\n");
      if((fp=fopen("team.dat","r")) == NULL) {
        printf("Error opening data file\n");
        return 0;
      }
    
      while(1) { //giving fscanf() it's own line
        got = fscanf(fp,"%s %s %s %c %d %d", fname[i],lname[i],team[i],&plays[i],&data[i][0],&data[i][1]); 
        if(got < 6)
          break;
     
        index[i] = i;  //you're building an index array at the same time? Good!    
        ++i; //welcome to C! :)     = i + 1;
        
      }
      //i has just counted the number of names in the array, so it can be used:
    
      printf("\n\n");
      for (j = 0; j < i; j++) {  //start with 0 not 1
        printf("%s %s %s %c %d %d\n",fname[j],lname[j],team[j],plays[j],data[j][0],data[j][1]);
      }
    
      sort(index, fname,1);
      printf("\n\n");
      for (j = 0; j < MAX; j++) {  //start with 0
        i = index[j];    
        printf("%11s   %-11s %4s  %c  %2d  %2d\n",fname[i],lname[i],team[i],plays[i],data[i][0],data[i][1]);
      }
    
    
      fclose(fp);
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    void sort(int index[MAX], char fname[][22], int key) {
      int i, j, temp;
        
      if(key==1) { //sort by first name
        /* This is substitution sort - similar to 
        bubble sort, but slightly faster
        */
        for(i=0;i<MAX-1;i++) {
          for(j=i+1;j<MAX;j++) {
            /*compare strings i with j, using the index numbers
              no actual data is swapped out, with this index sort
            */
    
            if(strcmp(fname[index[i]], fname[index[j]]) > 0) {
              temp = index[i];
              index[i] = index[j];
              index[j] = temp;
            }
          }
        } 
      }
      else if(key == 2) {
        //etc.
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable between cpp files
    By Todd88 in forum C++ Programming
    Replies: 28
    Last Post: 12-03-2008, 06:01 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21