unable to input string into a struct - c

I have a header file structs.h as follows:
#include <stdio.h>
typedef struct
{
char *name;
}
student;
In the file structs.c,
#include <stdio.h>
#include "structs.h"
int main()
{
student students[3];
scanf("%s", &students[0].name);
return 0;
}
The scanf statement gives an error since it says %s is of type char* but argument is of type char**. Removing the & gives a segmentation fault.
How else would I write this? It is a array within an array of struct variables but it should be able to take input...

Related

Struct variable doesn't work in main function in c

I'm trying to get a struct variable from another file in c. But when I do define a variable inside of other file and trying to printing this variable in main file it didn't work.
Main File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
int main()
{
struct tst t;
this_test(t);
printf("%s", t.string);
return 0;
}
Other File Header
#ifndef _TESST_H
#define _TESST_H
struct tst
{
char *string;
};
void this_test(struct tst t);
#endif
Other File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
void this_test(struct tst t)
{
t.string = "this is test";
}
When I tried to execute this program it print nothing. How can I solve this problem?
The structure passed as a parameter to the function is only a copy. To modify the structure of the main function in the this_test function, you must pass its address. It's like the scanf function where you have to pass the address of the variables you want to modify.
#include <stdio.h>
struct tst
{
char *string;
};
void this_test(struct tst *t)
{
t->string = "this is test";
}
int main()
{
struct tst t;
this_test(&t);
printf("%s", t.string);
return 0;
}

using struct variable from .h file in .c

shm_fileuploader.c:
#include "sharedMemory.h"
struct filesharing_struct temp()
{
printf("Enter the name of the file. ");
scanf("%s", &fname);
}
sharedMemory.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
//void* get();
const int SIZE = 40960;
const char *name = "obaloch_filesharing";
struct filesharing_struct{
char flag;
int size;
char content;
char fname[50];
};
//struct filesharing_struct temp;
I am getting an error saying "error: ‘fname’ undeclared" and im not sure why since fname is declared in my header file. Is there anyway i can use fname in the from the .h file in the .c file?
You need to declare a variable whose type is the structure, and scan into its fname member.
Then you can return the structure from the function, to satisfy the return type.
struct filesharing_struct temp()
{
struct filesharing_struct temp;
printf("Enter the name of the file. ");
scanf("%s", &temp.fname);
return temp;
}

Struct in a struct using typedef - C

How do I properly use one struct inside another struct using typedef in C?
This method doesn't work and I can't understand why:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *nume;
char *prenume;
data data;
} student;
typedef struct
{
int an,zi,luna;
} data;
int main()
{
student Andrei;
scanf("%s %s %d ",Andrei.nume,Andrei.prenume,&Andrei.b.an);
printf("%s %s %d ",Andrei.nume,Andrei.prenume,Andrei.data.an);
return 0;
}
There are actually a number of errors in your code!
First, you need to declare/define any struct object before you use that as a member of another struct.
Second, your nume and prenume members are declared as pointers to characters but they are not initialized to anything, nor is any memory allocated for those string.
Third, you have a 'typo' in your scanf line: Andrei.b.an should, presumably, be Andrei.data.an.
Finally (I think), because you have a trailing space in the format string for scanf, the function will need at least one 'extra' input field in order to complete.
Here is a potential 'fix', with comments added where I've made changes:
#include <stdio.h>
// <stdlib.h> // You don't use this - perhaps thinking of using "malloc" for "nume" and "prenume"??
typedef struct // This MUST be defined BEFORE it is used as a member in the following struct!
{
int an, zi, luna;
}data;
typedef struct
{
char nume[32]; // Here, we create fixed char arrays (strings) for "nume"...
char prenume[32]; // ... and "prenume". You can change the sizes, as you need!
data data;
} student;
int main()
{
student Andrei;
scanf("%s %s %d", Andrei.nume, Andrei.prenume, &Andrei.data.an); // Note: removed trailing space from format!
printf("%s %s %d", Andrei.nume, Andrei.prenume, Andrei.data.an);
return 0;
}

Pass structure array to function using pointers

I am trying to send a structure array as reference, but for some reason I cannot get it to work, as value it is able to pass it but not as reference (&)
This is my code:
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct record[])
{
printf ("YES, there is a record like %s\n", record[0].line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record);
return 0;
}
I thought that only by calling the function func(&record) and changing the func function arguments as "struct mystruct *record[]" it was going to work... but it didn't.
Any help please.
I think you've got your pointers and references concepts mixed up.
func(&record) would pass the address of the variable record and not a reference.
passing pointers
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct * record)
{
printf ("YES, there is a record like %s\n", record[0].line);
// OR
printf ("YES, there is a record like %s\n", record->line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record); // or func(&record[0])
return 0;
}
if you must pass a reference, try this
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct & record)
{
printf ("YES, there is a record like %s\n", record.line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record[0]);
return 0;
}
Update
To address the comment(s) below,
references are not available in pure C, available only in C++
the 'fault' in the original code was a struct mystruct record[] should have been struct mystruct & record

Struct Issue in C, using same Struct Multiple Source Files

I have a 'struct' which I would like to use in multiple sources files. I have declared the struct in the Header File and then have included in the sources files. It would be great if someone can assist me in this problem.
I am posting Header, Source and Error
#ifndef DATABASE_H
#define DATABASE_H
struct dataBase
{
char modelName;
float capacity;
int mileage;
char color;
};
extern struct dataBase Inputs;
#endif /* DATABASE_H */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "dataBase.h"
struct dataBase Inputs = NULL;
//size_t Inputs_Size = 0;
int main (void)
#include "hw4_asharma_display.h"
#include <stdio.h>
#include "dataBase.h"
void printLow(int size)
{
// Declaring Variables
int i;
for(i=0; i<size; i++)
{
printf("%s %f %d %s\n",
Inputs[i].modelName,
Inputs[i].capacity,
Inputs[i].mileage,
Inputs[i].color);
}
hw4_asharma_display.c:14:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
~~~~~~^~
hw4_asharma_display.c:29:23: error: subscripted value is not an array, pointer, or vector
Inputs[i].modelName,
Inputs is not an array, so you can't just use the [i] index notation. You'll have to change its declaration from:
struct dataBase Inputs = NULL;
(btw the NULL part is pointless) to
struct dataBase Inputs[N];
Instead, if you meant to have only one element, keep the declaration:
struct dataBase Inputs;
but remove the [i] part:
printf("%c %f %d %c\n",
Inputs.modelName,
Inputs.capacity,
Inputs.mileage,
Inputs.color);
Also, you will have to fill each element before printing or you will get all zeroes and blanks.

Resources