Thread: pointer problems

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    34

    pointer problems

    Hello, I'm having a problem with pointers that I can't figure out. It's hard for me to explain so I will show you the code and the problem.

    calling function prototype:
    Code:
    char * LoadAudioList(char * file_tracks)
    The above function has to return one of its local variables, here is the decleration:
    Code:
    static char playlist[TOTAL_SONGS][TRACK_LENGTH];
    The return statement:
    Code:
    return *playlist;
    The warning I get is from this line:
    Code:
    char (* playlist)[81];
    
    playlist = LoadAudioList("game_songs.txt");
    The warning I'm getting is:
    assignment makes pointer from integer without a cast
    ~flood

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Returning pointers to 2D arrays adds a new level of weirdness to the whole proceedings. Writing things out long-hand just makes no sense at all on the face of it. But in it's own weird way, it kind of makes sense if you stare at it long enough

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*#define USE_A_TYPEDEF*/
    #define TRACK_LENGTH 80
    #define TOTAL_SONGS 100
    
    #ifdef USE_A_TYPEDEF
    typedef char (*twod)[TRACK_LENGTH];
    twod LoadAudioList(char * file_tracks) {
      static char playlist[TOTAL_SONGS][TRACK_LENGTH];
      strcpy( playlist[0], file_tracks );
      return playlist;
    }
    #else
    char (*LoadAudioList(char * file_tracks))[TRACK_LENGTH] {
      static char playlist[TOTAL_SONGS][TRACK_LENGTH];
      strcpy( playlist[0], file_tracks );
      return playlist;
    }
    #endif
    
    int main(void)
    {
    #ifdef USE_A_TYPEDEF
      twod playlist;
    #else
      char (*playlist)[TRACK_LENGTH];
    #endif
      playlist = LoadAudioList("game_songs.txt");
      printf( "%s\n", playlist[0] );
      return 0;
    }
    Spare your sanity and choose the typedef approach - it makes everything so much more readable, and functions once again look like functions!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    I used the typedef approach and I still get the same warning however, if I try and compile your code everything works fine. The thing is that the program works the way it's supposed but the warning is bothering me.
    ~flood

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then you're still messing up the types somewhere.
    Post a bit of your actual code, showing your use of typedef and the error(s)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    Here is the original code

    aud.h

    Code:
    /* Audio.c functions     */
    bool InitAudio(int freq, Uint16 frmat, int chann, int buff);
    char * LoadAudioList(char * file_tracks); // The evil function here
    bool PlayTrackList(char playlist[][TRACK_LENGTH], int random);
    void NextAudioTrack(void);
    bool OpenAudio(char * audio_name);
    bool PlayAudio(int loop);
    void CleanupAudio(void);
    Code:
    char * LoadAudioList(char * file_tracks)
    {
        FILE * audio;
        static char playlist[TOTAL_SONGS][TRACK_LENGTH];
        int bytes = 0;
        
        if ((audio = fopen(file_tracks, "rb")) == NULL)
        {
            fprintf(logging, "Error when opening file: %s\n", file_tracks);
            return NULL;
        }
        
        while ((bytes < TRACK_LENGTH) && (playlist[total][bytes] = getc(audio)) != EOF && 
                total < TOTAL_SONGS)
        {
            if (playlist[total][bytes] == ';')
            {   
                playlist[total][bytes] = '\0';
                getc(audio);
                getc(audio);
                   
                total++;
                bytes = 0;
                
            }
            else
                bytes++;
        }
        
        if (playlist[total][bytes] == EOF)
        {
            playlist[total][bytes] = '\0';
        }
        
        fprintf(logging, "Total songs: %d\n", total);
        
        return *playlist;
    }
    Now using typedef

    aud.h

    Code:
    typedef char (*twod)[TRACK_LENGTH];
    
    /* Audio.c function     */
    bool InitAudio(int freq, Uint16 frmat, int chann, int buff);
    twod LoadAudioList(char * file_tracks);
    bool PlayTrackList(char playlist[][TRACK_LENGTH], int random);
    void NextAudioTrack(void);
    bool OpenAudio(char * audio_name);
    bool PlayAudio(int loop);
    void CleanupAudio(void);
    LoadAudioList in source file audio.c

    Code:
    twod LoadAudioList(char * file_tracks)
    {
        FILE * audio;
        static char playlist[TOTAL_SONGS][TRACK_LENGTH];
        int bytes = 0;
        
        if ((audio = fopen(file_tracks, "rb")) == NULL)
        {
            fprintf(logging, "Error when opening file: %s\n", file_tracks);
            return NULL;
        }
        
        while ((bytes < TRACK_LENGTH) && (playlist[total][bytes] = getc(audio)) != EOF && 
                total < TOTAL_SONGS)
        {
            if (playlist[total][bytes] == ';')
            {   
                playlist[total][bytes] = '\0';
                getc(audio);
                getc(audio);
                   
                total++;
                bytes = 0;
                
            }
            else
                bytes++;
        }
        
        if (playlist[total][bytes] == EOF)
        {
            playlist[total][bytes] = '\0';
        }
        
        fprintf(logging, "Total songs: %d\n", total);
        
        return playlist;
    }
    Here is where the function gets called and the warning comes up
    in another source file

    Code:
    typedef char (*twod)[81];
    
        twod playlist;
        /* Load audio playlist                                             */
        playlist = LoadAudioList("game_songs.txt");//The warning is here
        
        if (playlist != NULL)  
           PlayTrackList(playlist, 1);
    ~flood

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef char (*twod)[TRACK_LENGTH];
    > typedef char (*twod)[81];
    Just include the header file, then you'll be all nice and consistent.

    I mean, you need the header file to prototype the function anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    Yey it works now. Why do I need to include the header file if it's already included in audio.c which is one file in my project.
    ~flood

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to include the header EVERYWHERE you use declarations which are in that header file.
    I mean, I take it you #include <stdio.h> in all the files where you call say printf()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    The compiler works in a certain order: preprocessing, compiling, linking. Preprocessing comes first, meaning that the code from the header is put into the source file. Then compiling. Now, the typedef statement works something like a preprocessor definition. It's not the same, but basically, the typedef won't carry over after compiliation. So, they don't exist anymore when linking comes around.

    Now, depending on what is in your header, you may or may not need to include it multiple times.
    Code:
    void function(void)
     {
      function();
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM