There are quite a few topics on this subject but I haven't been able to find a solution that's worked for me; I am getting a segmentation fault whenever I try to change student_t.member->name. Below is the typedef structure that I am required to use:
typedef struct degree_t degree_t;
typedef struct student_t student_t;
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
char* name;
int age;
}
int main(int argc, char* argv[]) {
student_t *students = malloc(sizeof(student_t) * 3);
degree_t *degrees = malloc(sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member->name = "Bob";
}
return 0;
}
I can change degrees[i].course_id perfectly fine, but whenever I try to change degrees[i].member -> name, I get a segmentation fault.
GDB indicates that this is a result of the line degrees[i].member->name = "Bob", but I'm don't understand why, and I don't know how to effectively change the value of degrees[i].member->name
Oops, you managed to place four errors in that small program.
The signature of main() should be main(int argc, char *argv[]). argv is an array of strings found on the command line.
You allocate memory for one degree, but in the for-loop you initialize three of them. This leads to a heap corruption.
You don't initialize degrees->member, but use it.
You allocate memory for three students, but don't use it.
Try this:
struct degree_t {
student_t* member;
int course_id;
}
struct student_t {
const char* name;
int age;
}
int main(int argc, char* argv[]) {
degree_t *degrees = malloc(3 * sizeof(degree_t));
for (int i=0; i<3; i++) {
degrees[i].course_id = 1;
degrees[i].member = malloc(sizeof(student_t));
degrees[i].member->name = "Bob";
degrees[i].member->age = 23;
}
return 0;
}
Related
I apologise if this seems simple, I'm still learning and I'm new to C.
I have this as my struct:
struct Game{
char id;
char name[50];
char genre[20];
char platform[15];
char company[30];
float price;
int quantity = 10;
};
And this declared as a struct array:
struct Game gList[30];
I have a function where I'm passing all of 'gList' to search through values in the gList[i].name variables.
So my question is, is it possible to send only the gList[i].name part of the struct to the function as a parameter?(ie All the 30 name values only).
No.
But you could make an array of pointers that point to the name field and pass it to the function:
char* ptr[30];
for(int i = 0; i < 30; i++)
ptr[i] = gList[i].name;
func(ptr);
No you can't. However, you can pass iterators to functions just fine. Typical pattern:
struct context { struct Game *gList; int nList; int i; }
char *iter_names(void *baton)
{
struct context *ptr = baton;
if (ptr->i == ptr->nList) return NULL;
return ptr->gList[ptr->i++].name;
}
void wants_name_array(char (*nextname)(void *), void *baton)
{
while (char *name = nextname(baton))
{
printf("%s\n", name);
/* and whatever else you are doing */
}
}
/* ... */
struct context baton = { gList, 30, 0 };
wants_name_array(iter_names, baton);
Yeah it looks kinda bad. Thankfully, gcc has an extension that makes this much better.
void wants_name_array(char (*nextname)())
{
while (char *name = nextname())
{
printf("%s\n", name);
/* and whatever else you are doing */
}
}
/* ... */
{
int i = 0;
char *nextname()
{
if (i == 30) return NULL;
return gList[i++].name;
}
wants_name_array(nextname);
}
When using this particular gcc extension, never ever return nested functions. Undefined behavior.
I am trying to pass a string array and indexes from where to start end searching in the array, I am unable to solve it from the last two days. I am sending to the pthread_create a struct data thread_data, here i am able to send the int and long data, but not the string array, can someone help me, how to pass these.
struct data{
int tid;
unsigned long start;
unsigned long end;
char * word;
char * str;
};
struct data thread_data[NUM_THREADS];
void *searchString(void *passeddata)
{
struct data *t_data;
int tid1;
char * str[3];
t_data=(struct data *) passeddata;
tid1=t_data->tid;
str=t_data->str;
.....
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
...
char work[]={"First Line","Second line","Third line"};
...
while(fgets(arr[index],120, fp)!=NULL){
index=index+1;
thread_data[index].tid=index;
thread_data[index].str=work;
...
rc=pthread_create(&threads[index],NULL,searchString,(void *)&thread_data[index]);
...
}
pthread_exit(NULL);
}
To hold the multiple strings you need 2D array.
const char *work[]={"First Line","Second line","Third line", "Fourth Line"};
You need to use pointer to pointer in struct data to hold the above array.
struct data{
.....
const char **str;
size_t lenOfStr;
};
And pass the length of array explicitly to thread function from main function.
Your sample code may look like below.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct data{
int tid;
unsigned long start;
unsigned long end;
char * word;
const char **str;
size_t lenOfStr;
};
struct data thread_data[3];
void *searchString(void *passeddata)
{
struct data *t_data;
int tid1;
const char **str = NULL;
t_data=(struct data *) passeddata;
tid1=t_data->tid;
str=t_data->str;
int i = 0;
for (i = 0;i<t_data->lenOfStr;i++)
printf("%s\n", str[i]);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
const char *work[]={"First Line","Second line","Third line", "Fourth Line"};
int index = 0;
pthread_t threadid=0;
thread_data[index].tid=index;
thread_data[index].str=work;
thread_data[index].lenOfStr = sizeof(work)/sizeof(*work); // Calculate the size of work here
int rc=pthread_create(&threadid,NULL,searchString,&thread_data[index]);
pthread_exit(NULL);
}
I'm trying to compile my c code, but I always get Segmentation fault after I execute my program. Here is part of my code:
LINE_LENGTH=300
struct clip {
int views;
char *user;
char *id;
char *title;
char *duration;
struct clip *next;
} *head;
my main function, where argv[1] is my csv file
int main(int argc, char **argv) {
int n;
head = build_a_lst(*(argv+1));
return 0;}
The rest of my code
struct clip *build_a_lst(char *fn) {
FILE *fp;
struct clip *hp;
char *fields[5];
char line[LINE_LENGTH];
int cnt=0,i;
hp=NULL;
fp=fopen(fn,"r");
if(fp=NULL)
exit(EXIT_FAILURE);
while(fgets(line,LINE_LENGTH,fp)!=NULL){
split_line(fields,line);//fields has five values stored
hp=append(hp,fields);
for(i=0;i<5;i++){
free(fields[i]);
fields[i]=NULL;
}
}
return hp;
}
void split_line(char **fields,char *line) {
int i=0;
char *token, *delim;
delim = ",\n";
token=strtok(line,delim);//ok line
for(;token!=NULL;i++){
fields[i]=malloc(strlen(token)+1);
strcpy(fields[i],token);
token=strtok(NULL,delim);
}
}
struct clip *append(struct clip *hp,char **five) {
struct clip *cp,*tp;
tp=malloc(sizeof(struct clip));
tp->views=atoi(five[1]);
tp->user=malloc(strlen(five[0]+1));
tp->duration=malloc(strlen(five[2]+1));
tp->id=malloc(strlen(five[3]+1));
tp->title=malloc(strlen(five[4]+1));
strcpy(tp->user,five[0]);
strcpy(tp->duration,five[2]);
strcpy(tp->id,five[3]);
strcpy(tp->title,five[4]);
cp=hp;
while(cp!=NULL)
cp=cp->next;
cp->next=tp;
hp=cp;
return hp;
}
According to some articles segmentation fault is caused by trying to read or write an illegal memory location. And since I am allocating memory in different parts of my code, the problem should be there. Can someone please help me with this. Thank you in advance.
There are some problems with your code:
if(fp=NULL) should be if(fp == NULL)
char *fields[5]; should be char *fields[5] = {NULL};
for(;token!=NULL;i++){ should be for(; token != NULL && i < 5; i++){
These:
tp->user=malloc(strlen(five[0]+1));
tp->duration=malloc(strlen(five[2]+1));
tp->id=malloc(strlen(five[3]+1));
tp->title=malloc(strlen(five[4]+1));
should be
tp -> user = malloc(strlen(five[0]) + 1);
tp -> duration = malloc(strlen(five[2]) + 1);
tp -> id = malloc(strlen(five[3]) + 1);
tp -> title = malloc(strlen(five[4]) + 1);
You don't free several of the malloced memory.
Say I have a simple struct, such as this one:
struct myStruct {
uint8_t arr[10];
};
All I want to be able to do is to modify the contents of that array. However, it seems that I cannot assign the array directly (ie, I can't do something like pointerToThisStruct->arr = anArrayofSizeTen).
So here is my main method:
int main(int argc, char **argv) {
uint8_t test[10] = {0};
myStruct *struc;
struc->arr = test; //can't do this
memcpy(struc->arr, test, sizeof(test));
}
Now, I understand that direct copying over won't work, but why is memcpy also giving me a segfault? How exactly am I supposed to modify the struct array?
You need to declare an actual myStruct. Not a pointer to one. Declaring a pointer to one doesn't actually allocate any memory for the struct.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
struct myStruct {
uint8_t arr[10];
};
int main(int argc, char **argv) {
int i;
uint8_t test[10] = {0,1,2,3,4,5,6,7,8,9};
struct myStruct struc;
memcpy(struc.arr, test, sizeof(struc.arr));
printf("struc.arr[] = ");
for( i=0; i < sizeof(test); i++ )
{
printf("%d ", struc.arr[i]);
}
printf("\n");
return( 0 );
}
You are getting a segmentation fault because you didn't allocate your struct pointer.
int main(int argc, char **argv) {
uint8_t test[10] = {0};
struct myStruct *struct = malloc(sizeof(struct myStruct));
if (!struc)
return -1;
memcpy(struc->arr, test, sizeof(test));
free(struc);
return 0;
}
But, as #Chimera mentioned, you perfectly can not use a point and directly a heap-allocated structure, and access to its inner fields with the . operator
I am new to c programming. Could anyone please tell me what's wrong with
the following program?
typedef struct Person_s
{
int age;
char name[40];
} Person_t;
int process_list(int *countReturned, Person_t **p_list)
{
Person_t *rowPtr=0;
//the actual program will fethc data from DB
int count =1;
if(!((*p_list) = (Person_t *) malloc(sizeof(Person_t))))
{
return -1;
}
rowPtr = *p_list;
rowPtr[count-1].age =19;
strcpy(rowPtr[count-1].name,"Prince Dastan");
*countReturned = count;
return 0;
}
int main(int argc, char *argv[])
{
Person_t *tmpPerson=0;
Person_t **p_list=0;
int *count=0;
int i;
process_list(count,p_list);
tmpPerson = *p_list;
for(i=0; i< *count; i++)
{
printf("Name: %s , age: %d\n",tmpPerson->name,tmpPerson->age);
tmpPerson++;
}
//free(tmpPerson);
return 0;
}
Your problem is that you're setting the pointers to point to NULL (0), then dereferencing them. You are not allowed to dereference NULL. Want you want is more like this:
int main(int argc, char *argv[])
{
Person_t tmpPerson;
Person_t *p_list=0;
int count;
int i;
process_list(&count, &p_list);
tmpPerson = *p_list;
// and so on...
The & is the "address-of" operator, which returns a pointer to the variable's address. So this passes a pointer to count and p_list, which your function then uses to set those variables, which appears to be what you want to do.
You should have in main:
Person_t *p_list=0;
...
process_list(count, &p_list);
The code as written passes in 0 to process_list, and then you have:
*0 = (Person_t *)malloc(...);
This causes 0 to be dereferenced, and your code will crash.
The value of p_list as you enter the function is 0. If you dereference 0 you get a Bus Error.
if(!((*p_list) = (Person_t *) malloc(sizeof(Person_t))))
(90% of C problems are caused by dereferencing a null pointer, just like 90% of Java problems are caused by a misconfigured classpath. :-)