Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <string.h>
typedef struct Pessoa{
char nome[150];
struct Datanasc{
int dia;
int mes;
int ano;
} datanasc;
char genero;
char pref;
} pessoa;
typedef struct Aresta{
int *pont;
int *pont2;
int peso;
} aresta;
void leitura(struct Pessoa c1){
printf("Entrei sua vagabunda\n");
int diaatual,mesatual,anoatual;
int numeroalunos;
int i;
scanf("%d/%d/%d",&diaatual,&mesatual,&anoatual);
scanf(" %d",&numeroalunos);
for(i=0;i<numeroalunos;i++){
scanf(" %[^\n]s", pessoa[i].nome);
scanf(" %d/%d/%d", &pessoa[i].datanasc.dia,&pessoa[i].datanasc.mes,&pessoa[i].datanasc.ano);
scanf(" %c %c", &pessoa.genero[i],&pessoa.pref[i]);
}
for(i=0;i<numeroalunos;i++){
printf(" %[^\n]s", pessoa[i].nome);
printf(" %d/%d/%d", pessoa[i].dia,pessoa[i].mes,pessoa[i].ano);
printf(" %c %c", pessoa[i].genero,pessoa[i].pref);
}
return;
}
int main(){
pessoa c1;
leitura(c1);
return 0;
}
What's wrong with my code?
error in line 29, 30, 31, 35, 36, 37-expected expression before "pessoa"
I am having trouble with my structures. Probably it's something related to the [i]'s I have
In your code, pessoa is a type, not a variable name. So, you cannot possibly write
scanf(" %[^\n]s", pessoa[i].nome);
You may want to use a variable of type pessoa.
Having said that, C uses pass-by value for function parameter passing. They way you pass c1 (a normal variable) from main() and try to populate that in leitura() is not going to serve what you probably want. You need to pass an array of type pessoa from main() and use that inside leitura().
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to store name id in a structure. then I want to print them. Condition is if there is no store data in structure print "No data". But I can't do this in if condition.
#include<stdio.h>
struct store
{
char name[100];
int id[50];
} info[100];
int main()
{
int i=0;
printf("Enter your name\n");
scanf("%s",info[i].name);
printf("Enter your ID\n");
scanf("%d",info[i].id);
if(info.name[i]!='\0')
{
printf("%s",info[i].name);
}
else
{
printf("No data found");
}
}
When you refer to an array by its name only, it decays into a pointer to its 1st element. So info.name will not compile, it would need to be info->name instead. However, everywhere other than your if statement, you are using info[i].name to access the name of a specific element at index i, which is fine, but then you use info.name[i] in the if statement, which is not fine. See the difference? To access the 1st char of a name of a specific element, you would need info[i].name[0] instead.
Also, you are declaring the id field as an array of ints, but you really only need 1 int.
Also, you are not initializing the info array before filling it with data.
Try something more like this instead:
#include <stdio.h>
#include <string.h>
struct store
{
char name[100];
int id;
} info[100];
int main()
{
memset(info, 0, sizeof(info));
int i = 0;
printf("Enter your name\n");
scanf("%99s", info[i].name);
printf("Enter your ID\n");
scanf("%d", &(info[i].id));
if (info[i].name[0] != '\0')
{
printf("%d %s", info[i].id, info[i].name);
}
else
{
printf("No data found");
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I am having a problem when compiling this program, I just can't get it to work, I mean if I put the inputstudent() code inside the main(), it is much easier but I have to place the code in a function, inputstudent() and call in from the main(). I know it sounds very easy but I can't get it.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent();
void inputstudent();
int main(){
struct student s;
inputstudent(s);
displaystudent(s);
return 0;
}
void inputstudent(struct student s){
printf("Enter the surname: ");
scanf("%s",s.surname);
printf("Enter the other name: ");
scanf("%s",s.oname);
printf("Enter the age: ");
scanf("%d",&s.age);
printf("Enter the address: ");
scanf("%s",s.address);
}
void displaystudent(struct student s)
{
printf("Surname: %s \n",s.surname);
printf("Oname: %s \n",s.oname);
printf("Age: %d \n",s.age);
printf("Address: %s",s.address);
}
In C parameters are passed by value, so any modifications made to a parameter inside the function will be local modifications.
Lets have a look at following code snippet which is basically a very simple version of what you're trying to do:
void GetNumber(int number)
{
printf("Type a number:\n");
scanf("%d", &number); // modifies the local variable `number`?
}
...
int n = 0;
GetNumber(n);
Now what is the value of n right after the call to GetNumber?
Well it's not the number the user has typed, but it's still 0, that is the value n contained prior to the call to GetNumber.
What you need is this:
void GetNumber(int *pnumber)
{
printf("Type a number:\n");
scanf("%d", pnumber); // modifies the value pointed by the pointer pnumber
}
...
int n = 0;
GetNumber(&n); // &n is the memory address of the variable n
You need to read the chapter dealing with pointers in your C textbook.
Other less important problem
Your prototypes
void displaystudent();
void inputstudent();
don't match the corresponding functions.
You are passing your struct by value, that's why the function is not modifying it. You should change your function that is intended to modify the struct to take a struct pointer as argument:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student s);
void inputstudent(struct student *s);
int main() {
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}
void inputstudent(struct student *s) {
printf("Enter the surname: ");
scanf("%s", s->surname);
printf("Enter the other name: ");
scanf("%s", s->oname);
printf("Enter the age: ");
scanf("%d", &s->age);
printf("Enter the address: ");
scanf("%s", s->address);
}
void displaystudent(struct student s) {
printf("Surname: %s \n", s.surname);
printf("Oname: %s \n", s.oname);
printf("Age: %d \n", s.age);
printf("Address: %s", s.address);
}
As mentioned by Michael Walz, use pointers in order to modify structure in function calls. Moreover your function signature and definition does not match that's why compiler is complaining:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student* pStudent);
void inputstudent(struct student* pStudent);
int main() {
struct student aStudent;
inputstudent(&aStudent);
displaystudent(&aStudent);
return 0;
}
void inputstudent(struct student* pStudent){
printf("Enter the surname: ");
scanf("%s", pStudent->surname);
printf("Enter the other name: ");
scanf("%s", pStudent->oname);
printf("Enter the age: ");
scanf("%d", &pStudent->age);
printf("Enter the address: ");
scanf("%s", pStudent->address);
}
void displaystudent(struct student* pStudent)
{
printf("Surname: %s \n", pStudent->surname);
printf("Oname: %s \n", pStudent->oname);
printf("Age: %d \n", pStudent->age);
printf("Address: %s", pStudent->address);
}
You seem to want the changes that you make to the structure variable s inside inputstudent() to be reflected back to the original variable. In that case you need to pass the address of the variable to the function instead of its value.
If you pass the value of s to the function instead of its address, a new copy of s would be made inside inputstudent() and the values would be read into this copy while the s in main() remains unchanged.
To solve this you give inputstudent() a pointer to the s in main() and make inputstudent() use this address while reading the data. In this way the changes made for the variable in inputstudent() will be reflected back to the s in main().
Call the function like
inputstudent(&s);
And to access members of a structure variable using a pointer to it, you use the -> operator instead of the . operator.
void inputstudent(struct student *s){
printf("Enter the surname: ");
scanf("%s",s->surname);
printf("Enter the other name: ");
scanf("%s",s->oname);
printf("Enter the age: ");
scanf("%d",&s->age);
printf("Enter the address: ");
scanf("%s",s->address);
}
Also, an address possibly involves white spaces in which scanf() won't do. You could use fgets() for that.
There are basically two causes for your problem.
Function declarations don't have any parameters.
The structure is passed by value instead of reference to inputstudent function.
You can solve both of them by changing the function prototypes in both declaration and definition to
void displaystudent(struct student s);
void inputstudent(struct student &s);
You can't use struct student s as parameter in inputstudent(). It is just a value copy.
You should use pointer as parameter.
As:
void inputstudent(struct student* s)
{...}
int main(){
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i am not getting output for this program.
// Pointer and structure
I want get output as 1Jatin. but for refreshing pointer by using structure. i cant do that. It working as int and float. but not as int and char. any one please solved out this.
#include <stdio.h>
struct name{
int a;
char b;
};
int main(){
struct name *ptr ,p;
ptr = &p;
printf("Enter integer:");
scanf("%d", &(*ptr).a);
printf("Enter name:");
scanf("%s", &(*ptr).b);
printf("Displaying:");
printf("%d%s",(*ptr).a,(*ptr).b);
return 0;
}
#include <stdio.h>
#define MAX_NAME_LENGTH 32
struct name{
int a;
char b[MAX_NAME_LENGTH];
};
int main(){
struct name *ptr ,p;
ptr = &p;
printf("Enter integer:");
scanf("%d", &ptr->a);
printf("Enter name:");
scanf("%s", ptr->b);
printf("Displaying: ");
printf("%d %s\n",ptr->a,ptr->b);
return 0;
}
Many things:
You can simply us -> operator to dereference pointer to its members
C-Strings are null terminated arrays of chars. That means that b member must be an array large enough to store a name characters + null terminator ('\0', 0x00, 0).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm making a fun little text-based game for fun and for some reason some text aren't showing up for the username.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name);
void printMainMessage(char* name);
int main(char* username) {
askCharacterName(username);
char* temp;
temp = &username;
printMainMessage(temp);
return (0);
}
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
void printMainMessage(char *name) {
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Here is my output:
Welcome []. Welcome to Lamescape!
A couple of things:
Your main function should have a different signature.
Consider allocating your memory at a higher level so it is available in lower
levels of the program.
When modifying buffers in c, always pass their size too.
Functions with void return values are not expected to return anything.
After fixing these problems your errors went away.
#include <stdio.h>
#include <stdlib.h>
/* Forward declarations -- Prototypes */
void askCharacterName(char *name, unsigned size);
void printMainMessage(char* name, unsigned size);
int main()
{
char namebuffer[100];
askCharacterName(namebuffer, 100);
printMainMessage(namebuffer, 100);
return 0;
}
void askCharacterName(char *name, unsigned size)
{
printf("What is your desired username?");
scanf("%s", name);
}
void printMainMessage(char *name, unsigned size)
{
printf("Hello %s. Welcome to Lamescape!\n", name);
}
Passing the size has no immediate effect here. I leave it up to you to figure out how to ensure that the buffer is never used beyond its bounds.
The username in askCharacterName has its memory content at the program stack.
void askCharacterName(char *name) {
char username[20];
printf("What is your desired username?");
scanf("%s", &username);
return *username;
}
Allocating the space in heap for username in main looks to be what you are looking for.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
when i execute it the function do not work why?
#include<stdio.h>
struct stack{
int x[10];
int last;
};
void init(struct stack *s)
{
s->last=0;
}
void insert(struct stack *s)
{
int a;
while(a!=0)
{
int i;
printf("Enter the value\n");
scanf("%d",&i);
s->last++;
s->x[s->last]=i;
printf("%d",s->x[s->last]);
printf("enter 1 to continue 0 to exit\n");
scanf("%d",&a);
}
}
int main()
{
struct stack s;
int y,z;
printf("Trying out stacks\n");
printf("\n______________\n");
init(s);
insert(s);
return 0;
}
In function insert(), you declared
int a;
and then without initializing a you are doing the following,
while(a!=0)
will give Undefined Behaviour.
The following lines can leads buffer overflow,
s->last++;
s->x[s->last]=i; // no restriction applied on last
last can be more than 9 which can cause buffer overflow as x[10].