assignment from incompatible pointer type (struct) c - pointers

for the struct
typedef struct Recording_Settings recording_settings;
struct Recording_Settings
{
gchar *profile;
gchar *destination;
};
recording_settings rec_settings;
I get a warning when I try to do this
static void profile_combo_change_cb(GtkComboBox *combo, gpointer userdata)
{
GtkTreeIter iter;
GtkTreeModel *model;
/* Grab the encoding profile choosen */
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) {
gchar *media_type;
gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &media_type, -1);
rec_settings.profile = rb_gst_get_encoding_profile(media_type); // Warning: assignment from incompatible pointer type
g_free (media_type);
}
}
Am I misunderstanding or missing something?
Thanks.

The type of rb_gst_get_encoding_profile seems to be
GstEncodingProfile *rb_gst_get_encoding_profile (const char *media_type);
but you assign its return value to a gchar *.
GstEncodingProfile is a struct type, as far as I can determine (typedef struct _GstEncodingProfile GstEncodingProfile;), and gchar is probably a typedef for a character type (most likely typedef char gchar; from glib). So the types would be incompatible.

Related

X is not a valid template argument for 'const char*' because it is not the address of a variable

I'm trying to use the resources of a temporary class object as a template parameter. But apparently this doesn't work:
godbolt
#include <iostream>
constexpr size_t size(const char* s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
}
return i;
}
template <const char* S>
struct string_struct
{
constexpr string_struct() {
for (int i=0; i < size(S); ++i) {
buf_[i] = S[i];
}
}
constexpr const char* get() {
return buf_;
}
char buf_[size(S)] = {0};
};
constexpr const char some_chars[] = "100";
constexpr auto compose_string()
{
string_struct<some_chars> other_str{};
return string_struct<other_str.get()>;
}
int main()
{
compose_string();
}
gcc 12.1 complains:
<source>: In function 'constexpr auto compose_string()':
<source>:32:41: error: 'other_str.string_struct<(& some_chars)>::get()' is not a valid template argument for 'const char*' because it is not the address of a variable
32 | return string_struct<other_str.get()>;
| ^
<source>:29:16: error: invalid return type 'auto' of 'constexpr' function 'constexpr auto compose_string()'
29 | constexpr auto compose_string()
|
Of course this is a contrived example. What I actually want to do is extend an enhanced version of string_struct recursively. But how can I use its resources for template instantiation? And if that is somehow possible, how do I deduce the return type of a function that returns string_structs (and in the recursive case a string_struct, that is instantiated with another string_struct, that is instantiated with another sstring_struct...)?
Is it even possible?

passing different structure type in c for functions

I have a following code :
typedef struct PStruct{
int len;
char* data;
}PointerStruct;
typedef struct AStruct{
int len;
char data[256];
}ArrayStruct;
void checkFunc(PointerStruct* myData)
{
if (0 == myData || 0 == myData->data){
printf("error\n");
}
}
int main()
{
ArrayStruct my_data;
my_data.len = 256;
char data[] = "data is sent";
my_data.data = &data;
checkFunc((PointerStruct*)my_data);
return 0;
}
is there any wrong in passing structure which has array. where as the required is pointer.
please let me know.
There are a couple of points to be considered in your program.
char data[] = "data is sent";
This is a character array of 13 characters. Hence, my_data.data = &data; will give a compilation error as shown below
error: incompatible types when assigning to type 'char[256]' from type 'char (*)[13]'
To copy your string, you could probably use strcpy as shown below
strcpy(my_data.data, data);
Next point is passing the pointer to the object. In this call, checkFunc((PointerStruct*)my_data);, you are passing the instance of the object to the function call, but are type-casting as a pointer. You would face compilation issues due to the mismatch of the datatypes as error: cannot convert to a pointer type
To overcome this error, you should pass a reference to your my_data object as checkFunc((PointerStruct*) &my_data);. Hence, your new main function would look like
int main()
{
ArrayStruct my_data;
my_data.len = 256;
char data[] = "data is sent";
//my_data.data = &data;
strcpy(my_data.data, data); // Use of strcpy. You would require to include <string.h>
checkFunc((PointerStruct*)(&my_data)); // Pass a reference and not by value
return 0;
}
With these changes, your code should work fine.

character pointer vs int pointer

How this works:
int main()
{
int * ch = NULL;
cout<<"Hello"<<ch<<"World"<<endl;;
cout<<"Hello world1"<<endl;
return 0;`
}
Whereas this fails:
int main()
{
char * ch = NULL;
cout<<"Hello"<<ch<<"World"<<endl;;
cout<<"Hello world1"<<endl;
return 0;`
}
For most pointer types,
cout << ptr;
prints an implementation-defined representation of the pointer value, usually an address in hexadecimal format. But for char*, the operator<<() is overloaded to interpret the pointer as a pointer to the first char in a 0-terminated char array, and print it like printf("%s", ptr); would.
Printing the address a null pointer points to is harmless, following a null pointer to interpret the bytes starting from where it points to is undefined behaviour, and more often than not leads to a segmentation fault.

initializing a typedef struct pointer to NULL

#include<stdio.h>
typedef struct student{
int id;
int mark;
}stud;
typedef struct stud *s1;
void main(){
s1 = NULL;
printf("hi");
}
Please help me how to initialize struct pointer to NULL. i get the following error during compilation.
graph.c: In function ‘main’:
graph.c:11:04: error: expected identifier or ‘(’ before ‘=’ token
You meant to define the variable s1 as
stud *s1;
Live demo: http://ideone.com/9ThCDi
The reason you got the error you did is that you were declaring s1 to be a type for "pointer to struct stud". This is wrong for two reasons:
You didn't want s1 to be a type; you wanted it to be an object.
Your struct was struct student. But you defined an actual type called stud.
Use struct student *s1;
instead of
typedef struct stud *s1;
as far as i know, typedef is used when you are defining your custom data type only.

Pointers in structure

I'm trying to use a file pointer that I have declared in a structure of linked list, but I keep getting it as a NULL value.
I have the following structure:
struct _hash_table
{
char found;
struct _hash_chain *hash_chain;
}
struct _hash_chain
{
uint64_t value;
FILE *fout;
struct _hash_chain *next;
}
and
struct _hash_table hash_table[TABLE_SIZE];
I keep getting hash_table[i]->hash_chain->fout = NULL and it's pointer address is nil.
Do I need to dynamically allocate memory for the pointer?
struct _hash_table hash_table[TABLE_SIZE]; - This will not allocate memory for struct _hash_chain because hash_chain is pointer variable in _hash_table.
...
struct _hash_table hash_table[TABLE_SIZE];
for (i = 0; i < TABLE_SIZE; i++);
{
hash_table[i].hash_chain = (struct _hash_chain *)malloc(sizeof(struct _hash_chain));
memset(hash_table[i].hash_chain, 0, sizeof(struct _hash_chain));
}
//Then do file open for TABLE_SIZE times
//hash_table[0].hash_chain->fout = fopen("file.txt", "w");
...
Accssing h_table[i].hash_chain without dynamic memory allocation will leads to crash(an undefined behaviour). I hope you will take care of next pointer.

Resources