I'm trying to write a car register with different function, in different files, lib.c, lib.h and main.c. However when I try to compile the program it shows this error with my car "unknown type name ‘Car’
34 | void addCar(Car* car)":
In my 'lib.c' file, this is my progress so far:
#include "lib.h"
void printMeny() {
//meny
printf("Meny\n");
printf("1. Lägg till ett fordon\n");
printf("2. Ta bort ett fordon\n");
printf("3. Sortera efter bilmärka\n");
printf("4. Information om ett fordon\n");
printf("5. Skriv ut hela registret\n");
printf("0. Avsluta programmet\n");
}
int getNumber() {
char buffer[10] = {0};
int number = 0;
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer, "%d", &number);
return number;
}
void getString(char* buf, size_t len) {
if(fgets(buf,len,stdin)) {
char* p;
if((p = strchr(buf, '\n')) != NULL)
{
*p = '\0';
}
}
}
void addCar(Car* car) {
printf("Namn:\n");
getString(car->owner.name, sizeof(car->owner.name));
printf("Ålder:\n");
car->owner.age = getNumber();
printf("Märke:\n");
getString(car->car_brand, sizeof(car->car_brand));
printf("Modell:\n");
getString(car->car_model, sizeof(car->car_model));
printf("Registreringsnummer:\n");
getString(car->plate_num, sizeof(car->plate_num));
}
Then in my lib.h:
//#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAR_MAX 10
typedef struct {
char name[40];
int age;
} Person;
typedef struct {
Person owner;
char car_brand[20];
char car_model[20];
char plate_num[20];
} Car;
void printMeny();
int getNumber();
void addCar(Car* car);
void getString(char* buffer, size_t len);
And finally in main.c;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib.h"
int main() {
int val = 0;
Car cars[CAR_MAX] = {0};
int numberOfCars = 0;
printMeny();
val = getNumber();
switch(val) {
case 0:
break;
case 1:
addCar(&cars[numberOfCars]);
numberOfCars++;
Related
I have some basic question about the pointers, I have a char array and I am extracting some words my char array with using function and I want to return value and print it main I have Code But it's not working, thanks for your help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char cumle[30];
char kelimecikart(char *cumle,char *sozcuk);
int main(){
int i;
char sozcuk[30];
printf("sentence:\n");
gets(cumle);
puts(cumle);
printf("What is the word you want to extract of:\n");
gets(sozcuk);
puts(sozcuk);
printf("\n");
cumle[0]=kelimecikart(cumle,sozcuk);
for(i=0;i<17;i++){
printf("%c",cumle[i]);
}
}
char kelimecikart(char *dizi,char *cikansoz)
{
int a = strlen(dizi);
int b = strlen(cikansoz);
int i,j,tmp=0;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
if(*(dizi+i+j)==*(cikansoz+j)){
tmp++;
}
else{
break;
}
}
if(tmp==b){
i+=tmp-1;
}
else{
*(cumle+i)=*(dizi+i);
}
tmp=0;
}
return *cumle;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char cumle[30];
void kelimecikart(char *cumle,char *sozcuk);
int main(){
int i;
char sozcuk[30];
printf("sentence:\n");
gets(cumle);
puts(cumle);
printf("What is the word you want to extract of:\n");
gets(sozcuk);
puts(sozcuk);
printf("\n");
kelimecikart(cumle,sozcuk);
for(i=0;i<17;i++){
printf("%c",cumle[i]);
}
}
void kelimecikart(char *dizi,char *cikansoz)
{
int a = strlen(dizi);
int b = strlen(cikansoz);
int i,j=1;
for(i=0;i<a;){
if(j==0){
i++;
}
else if(j==b){
break;
}
for(j=0;j<b;j++){
if(dizi[i]==cikansoz[j]){
i++;
}
else{
break;
}
}
}
for(;i<a;i++){
dizi[i-b]=dizi[i];
}
return ;
}
//
Can you help me with passing elements of struct into a function (void show_info_abt_bus) for outputting my information? I don't understand how I should pass those elements.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void my_free(info_bus **,int);
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
if(!((*b)=(info_bus *)malloc(n*sizeof(info_bus)))){
printf("Error memory\n");
exit(0);
}
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n){
int i;
for (i = 0; i < n; i++) {
free((*b)[i]);
}
free(b);
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(b,n);
return 0;
}
You need to pass structure object Pass by reference in function.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void show_info_bus_abt_bus(info_bus *b,int n){
int i;
for (i=0;i<n;i++){
printf("\n===============================================");
printf("\n[%d].the number of a bus: %d",i+1,b[i].number);
printf("\n[%d]. Begin at: %d am",i+1,b[i].begin);
printf("\n[%d]. Finishes at: %d pm",i+1,b[i].end);
printf("\n[%d]. Stations: %s",i+1,b[i].stations);
printf("\n[%d]. Time working: %d",i+1,b[i].time_working);
printf("\n===============================================\n");
}
}
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
(*b)=(info_bus *)malloc(n*sizeof(info_bus));
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n)
{
int i;
for (i = 0; i < n; i++) {
free((*b)[i].stations);
}
free((*b));
(*b)=NULL;
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(&b,n)
return 0;
}
I've defined a header file with a struct and function prototypes that take a pointer to a struct as a parameter. The code compilation goes fine except that struct instantiated in the main do not seem to retain numerical data.
This is the header file:
#ifndef _GETDATA
#define _GETDATA
#include <stdio.h>
struct PERSONDATA{
char name[20];
double age,mass;
};
typedef struct PERSONDATA person;
extern void getData(person *);
extern void getName(char *,int);
#endif
This is the getData.c file:
#include <stdio.h>
#include "GETDATA.h"
void getData(person *ptr)
{
printf("Enter name: ");
getName(ptr->name,sizeof(ptr->name));
printf("enter age: ");
scanf("%f",&(ptr->age));
printf("enter mass: ");
scanf("%f",&(ptr->mass));
}
and this is the getName.c file:
#include <stdio.h>
#include "GETDATA.h"
void getName(char *ptrName, int varSize)
{
int i=0;
do
{
*(ptrName++) = getchar();
++i;
if(i==varSize) printf("array full, EXITING!\n");
}while(*(ptrName-1)!='\n' && i<varSize);
*(ptrName-1) = '\0';
}
The main function is as follows:
#include <stdio.h>
#include "GETDATA.h"
int main(int argc, char **argv)
{
person human1;
printf("hello, world!\n\n");
getData(&human1);
printf("\nData entered: \n");
printf("\tname = %s\n",human1.name);
printf("\tMass = %f\n",&(human1.mass));
printf("\tAge = %f\n",&(human1.age));
return 0;
}
This is the console output when the code is run:
What could be going wrong here?
Your values are doubles, not floats. You need to use %lf with scanf():
printf("enter age: ");
scanf("%lf",&(ptr->age));
printf("enter mass: ");
scanf("%lf",&(ptr->mass));
Also, your prints are wrong. You are passing a pointer. It should be
printf("\tMass = %f\n",human1.mass);
printf("\tAge = %f\n",human1.age);
I wonder if I'm doing something wrong in my program.
I manage to create a HashTable but when I send it through parameter to my displayingList() function, it crashes.
source.c (contains my functions):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include "header.h"
#define MAX 255
int countLetters(char myStr[])
{
int myLen = strlen(myStr), i;
int wordLen = 0;
for (i = 0 ; i < myLen; ++i)
{
wordLen += (int)(myStr[i]);
}
return (wordLen%256);
}
void populateList(NodeT *T[255], char myStr[])
{
NodeT *p, *q;
p = (NodeT *)malloc(sizeof(NodeT));
strcpy (p->key, myStr);
int myPos = countLetters(myStr);
if(T[myPos] == NULL)
{
p->next = NULL;
T[myPos] = p;
}
else
{
q = T[myPos];
p->next = q;
T[myPos] = p;
}
}
void displayList(NodeT *T[255])
{
int i;
NodeT *p;
for(i = 0 ; i < 255; ++i)
{
if(T[i] != NULL)
{
printf("Index: %d - Data:", i);
p = T[i];
while(p != 0)
{
printf("%s, ", p->key); // HERE IT CRASHES.
p = p->next;
}
printf("\n");
}
}
}
main.c (contains the int main()):
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(void)
{
NodeT *T[255];
int n, i;
printf("Give no. of elements:");
scanf("%d", &n);
fflush(stdin);
for(i = 0 ; i < n ; ++i)
{
char name[100];
gets(name);
populateList(T, name);
}
displayList(T);
return 0;
}
header.h (and my header):
#ifndef HEADER_H
#define HEADER_H
typedef struct cell
{
char key[100];
struct cell *next;
}NodeT;
int countLetters(char myStr[]);
void populateList(NodeT *T[], char myStr[]);
void displayList(NodeT *T[]);
#endif // HEADER_H
I tried to see what exactly happens with debugger and it seems that when I send T[] list to displayList() function, actually it doesn't have the same structure as it has in main.c.
ISSUE: the insertion works fine, but when I try to display my list (on each index) it crashes.
Any ideas?
Thanks in advance.
The possible solution is to declare the NodeT *T[255] global. However it isn't the best practice at all.
Can anyone give me some indication as to why array of structs doesn't print out properly ?
I think its something to do with the memory I have allocated to the struct I am unsure !!
Using mac osx mountain lion xcode 4 gcc
Thanks for any help completely stuck!!
(Please have patience I am only a student !)
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
typedef struct{
char* one;
char* two;
} Node;
Node *nodes;
int count = 0;
//-----------------------------------------------------------------------
void add(char *one,char*two){
char x[40];
char y[40];
printf("reached..\n");
strcpy(x,one);
strcpy(y,two);
printf("--> X: %s\n",x);
printf("--> Y: %s\n",y);
Node newNode;
newNode.one = x;
newNode.two = y;
nodes[count]= newNode;
count++;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void print(){
int x;
for (x = 0; x < 10; x++)
{
printf("%d : (%s, %s) \n",x,nodes[x].one, nodes[x].two);
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void check(char **arg)
{
if(strcmp(*arg, "Add") == 0)
{
add(arg[1],arg[2]);
}else if(strcmp(*arg,"print") == 0)
{
print();
}else{
printf("Error syntax Enter either: \n Add [item1][item2]\n OR \n print\n");
}
}
//-----------------------------------------------------------------------
void readandParseInput(char *line,char **arg)
{
if (fgets (line, 512, stdin)!= NULL) {
char * pch;
pch = strtok (line," \n\t");
int count = 0;
arg[0] = pch;
while (pch != NULL)
{
count++;
pch = strtok (NULL, " \n\t");
arg[count] =pch;
}
}else{
printf("\n");
exit(0);
}
}
//-----------------------------------------------------------------------
int main()
{
int size = 100;
nodes = calloc(size, sizeof(Node));
int i;
for(i = 0;i <100; i++){
printf("%s , %s \n",nodes[i].one,nodes[i].two );
// nodes[i].one = ".";
// nodes[i].two = ".";
}
char line[512]; /* the input line */
char *arg[50]; /* the command line argument */
while (1)
{
readandParseInput(line,arg);
if(arg[0] != NULL){
check(arg);
}
}
return(0);
}
You're keeping pointers to the following automatic variables:
char x[40];
char y[40];
These go out of scope when add() returns, leaving you with dangling pointers.
You either have to turn Node::one and Node::two into arrays, or allocate memory for them on the heap.
In you add() function, you cannot assign one struct to another via an = operator... you would have to copy it...
memcpy( &nodes[count], &newNode )
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *fn;
}NAME;
#define NAME_LEN 20
int main()
{
NAME name;
name.fn = (char *) calloc(NAME_LEN, sizeof(char));
strcpy(name.fn, "Namco");
printf("Name: %s\n", name.fn);
free(name.fn);
return 0;
}
you can't just assign a string like this in c
newNode.one = x;
newNode.two = y;
what is newNode.one referring to???
at Function add
newNode.one = x;
newNode.two = y;
to
newNode.one = strdup(x);
newNode.two = strdup(y);