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.
Related
Is it possible to set the values of a struct pointer in a struct? I get an error and cannot typecast myStruct* into myStruct.
typedef struct {
int foo;
int bar;
} myStruct;
int main() {
myStruct *pS;
myStruct S1 = {0,0};
myStruct S2;
pS = S1;
S2 = pS; // I get an error there, cannot set struct pointer to a struct
}
So, in your example, you have pointer pS and regular variable S1.
A pointer is a variable that stores the memory address as its value.
Variable is the name of memory location.
So, the difference between regular variable is that variable stores value of an object, but pointer stores memory address of an object.
There are operators which allow getting object's address and getting object value by it's address:
Address-of operator &. &a will return address of object a.
Dereference operator *. *p will return object stored by address p.
Thus, in your code you should get two errors:
pS = S1; // error: Trying to assign struct value to a pointer
S2 = pS; // error: Trying to assign pointer to a struct value
To fix this, you should assign address to a pS and value to S2
typedef struct {
int foo;
int bar;
} myStruct;
int main() {
myStruct *pS;
myStruct S1 = {0,0};
myStruct S2;
pS = &S1; // getting address of S1
S2 = *pS; // getting value stored by address pS
}
I am trying to pass a structure of point array as shown, how can I correctly dereference the address to change the value the address points to.
// header file "header.h"
typedef struct {
double x;
double y;
} Pointbase;
typedef Pointbase *XYpt;
typedef struct {
XYpt xy[1];
} ChartPointsbase;
typedef ChartPointsbase **PointArray;
#include "header.h"
...
void npCluster(double drop, XYpt *newpt, PointArray outpts)
{
double xx[2]={-15, 100};
int i;
outpts = (PointArray)malloc(sizeof(PointArray) * 2);
for (i=0;i<2; i++)
{
(*(*outpts)->xy[i])->x=xx[i];
(*(*outpts)->xy[i])->y=drop;
}
}
The complier likes the following line but does not compute
(*outpts)->xy[i]->y=drop;
Any suggestions will be most appreciated.
I figured it out for "c" compiler as follows:
Define struct with two 1D arrays each of size dimsize, allocate memory to handles, set the size =k, and dereference as follows:
for (i=0; i<k; i=i++)
{
(*(outpts->xx))->dat[i]=135*i+j;
(*(outpts->yy))->dat[i]=drop;
}
For further nesting, say struct array of the above with two unequal point arrays, where cht is an array of PointArray
typedef struct {
int32 dimSize;
C1Hdl cht[1];
} XYchartCluster;
// initialize 1st array
for (i=0; i<k; i=i++)
{
(*(*(xycht)->cht[0])->xx)->dat[i]=135*i+j;
(*(*(xycht)->cht[0])->yy)->dat[i]=drop;
}
// initialize 2nd array with values from point npt
for (i=0; i<sz; i=i++)
{
(*(*(xycht)->cht[1])->xx)->dat[i]=npt->x;
(*(*(xycht)->cht[1])->yy)->dat[i]=npt->y;
}
/*
Note: size of each array in chart should be initialized and
memory assigned (dynamically changing size)
*/
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.
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.
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.