Having trouble passing a pointer to a struct to other functions.
17 replies, posted
Here are my headers:
kajamtag.h
[cpp]
#ifndef _KAJAMTAG_H
#define _KAJAMTAG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "id3.h"
#include "ogg.h"
#ifdef _cplusplus
extern "C" {
#endif
struct kajamtag
{
char* title;
char* album;
char* artist;
char* track;
};
typedef struct kajamtag kajamtag_t;
int kajamtag_init(char*);
char* k_getTitle();
char* k_getAlbum();
char* k_getArtist();
#ifdef _cplusplus
}
#endif
#endif
[/cpp]
ogg.h
[cpp]
#ifndef _OGG_H
#define _OGG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kajamtag.h"
#define INIT_SIZE 50*sizeof(char)
int ogg_read(FILE*, struct kajamtag*);
int ogg_storeData(char*, struct kajamtag*);
#endif
[/cpp]
id3.h
[cpp]
#ifndef _ID3_H
#define _ID3_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>
#include "kajamtag.h"
#define TAG_TO_INT(tag) ((tag) &0x7f) | (((tag) &0x7f00) >> 1) | (((tag)&0x7f0000)>>2) | (((tag)&0x7f000000)>>3)
int id3_header(FILE*);
int id3_frame(FILE*, int, struct kajamtag*);
int id3_storeData(char*, char*, int, struct kajamtag*);
int id3_getFlag(int, int);
#endif
[/cpp]
My output from GCC:
[code]
gcc -c -Wall -fpic src/kajamtag.c src/id3.c src/ogg.c
In file included from src/kajamtag.h:27,
from src/kajamtag.c:21:
src/id3.h:37: warning: 'struct kajamtag' declared inside parameter list
src/id3.h:37: warning: its scope is only this definition or declaration, which is probably not what you want
src/id3.h:38: warning: 'struct kajamtag' declared inside parameter list
In file included from src/kajamtag.h:28,
from src/kajamtag.c:21:
src/ogg.h:36: warning: 'struct kajamtag' declared inside parameter list
src/ogg.h:37: warning: 'struct kajamtag' declared inside parameter list
src/kajamtag.c: In function 'kajamtag_init':
src/kajamtag.c:43: warning: passing argument 3 of 'id3_frame' from incompatible pointer type
src/kajamtag.c:54: warning: passing argument 2 of 'ogg_read' from incompatible pointer type
In file included from src/kajamtag.h:28,
from src/id3.h:32,
from src/id3.c:21:
src/ogg.h:36: warning: 'struct kajamtag' declared inside parameter list
src/ogg.h:36: warning: its scope is only this definition or declaration, which is probably not what you want
src/ogg.h:37: warning: 'struct kajamtag' declared inside parameter list
In file included from src/kajamtag.h:27,
from src/ogg.h:31,
from src/ogg.c:21:
src/id3.h:37: warning: 'struct kajamtag' declared inside parameter list
src/id3.h:37: warning: its scope is only this definition or declaration, which is probably not what you want
src/id3.h:38: warning: 'struct kajamtag' declared inside parameter list
mv *.o build
gcc -shared -o bin/libkajamtag.so build/kajamtag.o build/id3.o build/ogg.o
[/code]
No idea what I'm doing wrong. I've done some Googling, and this should work. :confused:
It seems like removing the "struct" keyword may fix it, from a very, very quick look.
I removed the struct keyword from id3.h and ogg.h, and from the function definitions from the .c files, and I get this:
[code]
gcc -c -Wall -fpic src/kajamtag.c src/id3.c src/ogg.c
In file included from src/kajamtag.h:27,
from src/kajamtag.c:21:
src/id3.h:37: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.h:38: error: expected declaration specifiers or '...' before 'kajamtag'
In file included from src/kajamtag.h:28,
from src/kajamtag.c:21:
src/ogg.h:35: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.h:36: error: expected declaration specifiers or '...' before 'kajamtag'
src/kajamtag.c: In function 'kajamtag_init':
src/kajamtag.c:43: error: too many arguments to function 'id3_frame'
src/kajamtag.c:54: error: too many arguments to function 'ogg_read'
In file included from src/kajamtag.h:28,
from src/id3.h:32,
from src/id3.c:21:
src/ogg.h:35: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.h:36: error: expected declaration specifiers or '...' before 'kajamtag'
In file included from src/id3.c:21:
src/id3.h:37: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.h:38: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.c:48: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.c: In function 'id3_frame':
src/id3.c:82: error: 'k_tags' undeclared (first use in this function)
src/id3.c:82: error: (Each undeclared identifier is reported only once
src/id3.c:82: error: for each function it appears in.)
src/id3.c:82: error: too many arguments to function 'id3_storeData'
src/id3.c: At top level:
src/id3.c:88: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.c: In function 'id3_storeData':
src/id3.c:92: error: 'k_tags' undeclared (first use in this function)
In file included from src/kajamtag.h:27,
from src/ogg.h:31,
from src/ogg.c:21:
src/id3.h:37: error: expected declaration specifiers or '...' before 'kajamtag'
src/id3.h:38: error: expected declaration specifiers or '...' before 'kajamtag'
In file included from src/ogg.c:21:
src/ogg.h:35: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.h:36: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.c:23: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.c: In function 'ogg_read':
src/ogg.c:44: error: 'k_tags' undeclared (first use in this function)
src/ogg.c:44: error: (Each undeclared identifier is reported only once
src/ogg.c:44: error: for each function it appears in.)
src/ogg.c:44: error: too many arguments to function 'ogg_storeData'
src/ogg.c: At top level:
src/ogg.c:59: error: expected declaration specifiers or '...' before 'kajamtag'
src/ogg.c: In function 'ogg_storeData':
src/ogg.c:80: error: 'k_tags' undeclared (first use in this function)
make: *** [build/kajamtag.o] Error 1
[/code]
[editline]07:57PM[/editline]
This is the same error that happened when I tried using a the typedef (kajamtag_t) I had defined in kajamtag.h.
[code]src/id3.h:37[/code]
The file you posted end with the line 17 o_O
Also, you can safely use forward declarations there.
[QUOTE=ZeekyHBomb;16008125][code]src/id3.h:37[/code]
The file you posted end with the line 17 o_O
Also, you can safely use forward declarations there.[/QUOTE]
I left out a comment at the top. :v:
What do you mean though? Those warnings don't mean anything?
They aren't warnings, anyway...
[QUOTE=gparent;16008510]They aren't warnings, anyway...[/QUOTE]
From the first output? The second one was errors.
Oh, sorry. At any rate, the warnings in the first output seem to be scary enough for it to not work... and I assumed it didn't.
just give up with this id3 and ogg shit.
[QUOTE=efeX;16009346]just give up with this id3 and ogg shit.[/QUOTE]
that'll get you far in life!
[QUOTE=gparent;16009684]that'll get you far in life![/QUOTE]
what
Cupcakes, your first lot of code compiled fine, albeit with warnings...
[QUOTE=CowsCanFly;16010398]Cupcakes, your first lot of code compiled fine, albeit with warnings...[/QUOTE]
Yeah, I didn't test it though, I don't know why. I usually treat warnings like errors.
[editline]11:41PM[/editline]
[QUOTE=efeX;16009346]just give up with this id3 and ogg shit.[/QUOTE]
This isn't about id3 or ogg anyway. I've already got it reading all that crap just fine.
The reason for passing around this struct is because before I had the struct object as a global variable. I was trying to integrate this into my music player, but it kept complaining about the name k_tags already being defined, which it wasn't. So I'm moving the struct object out of the global namespace.
What's your current code then? Fix up the lines or include the header-comment at the top to make the warnings and errors detectable more easily please :3
[QUOTE=PvtCupcakes;16010425]Yeah, I didn't test it though, I don't know why. I usually treat warnings like errors.[/QUOTE]
Which is good, because some of them lead to actual errors.
Okay found the problem. I looked through my C book, and it states:
[quote]
Remember that C passes arguments by value. Therefore, any time you pass a structure to a function, the function cannot make any permanent changes to the variable itself. It can only change a copy that is created when the function is called... /* stuff */
You are permitted to return an entire structure from a function... /* more stuff */
[/quote]
This book doesn't even mention pointers to structures, but they got crazy stuff like Linked Lists and pointers to pointers. :v:
I'll change my program to not use pointers, and just have it return a modified structure tomorrow.
[editline]11:17PM[/editline]
I lied, it does have pointers to structures, but all it says is "Hey look, you can use the '->' operator." Doesn't say a word about passing them to functions.
You can pass pointers to structs as arguments in C and modify them without problems actually.
Okay got it fixed.
Zeeky, you are right. I looked in my [b]other[/b] C book, and they were passing around struct pointers.
The problem was that my kajamtag.h file was including the other two headers, so when they got included they didn't know what a kajamtag struct was, so it thought I was defining a struct in the function parameters.
I just moved the includes below the struct kajamtag declaration/definition, and it compiles with no warnings.
Seems kind of hacky though. kajamtag.h needs id3.h and ogg.h so it can use those functions, and id3.h and ogg.h need kajamtag.h for the the kajamtag structure.
Sorry, you need to Log In to post a reply to this thread.