-
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:
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
-
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!
-
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.
-
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)
-
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);
-
> 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.
-
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.
-
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()
-
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.