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");
}
}
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 2 years ago.
Improve this question
I've declared a struct and a pointer array by the declared struct.
struct Book* Collection[100];
Before the code ends I want to free up my memory using free function. free function is called up after printf() at printCollection()
void printCollection(struct Book* col, int num) {
int i;
for (i = 0; i < num; i++) {
printf("Name: %s Author: %s Page: %d Price: %d\n", col[i].name, col[i].author, col[i].page, col[i].price);
free(col[i]);
}
}
but I can't even build the program because of a error. Could I get some help?
You need to change printCollection to look like this:
void printCollection(struct Book* col[], int num) {
int i;
for (i = 0; i < num; i++) {
printf("Name: %s Author: %s Page: %d Price: %d\n",
col[i]->name, col[i]->author, col[i]->page, col[i]->price);
free(col[i]);
}
}
Your original function signature only passes one pointer to the function when you need to pass an array of pointers.
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
I have used char *is[] to create array of strings. I have asked user for input of strings. i dont know where I have went wrong. IT IS SHOWING SEGMENTATION FAULT#include
#include <string.h>
#include <stdlib.h>
int main()
{
int count=0,p;
char *is[100];
for(int i=0;i<8;i++)
{
p=0;
scanf("%s",is[i]);
p++;
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(strcmp(is[i],sis[4+j])==0)
{
count=count+1;
}
}
}
if(count>=2)
{
printf("similar");
}
else{
printf("not similar");
}
}
char *is[100]; declares an array of char pointers. You need to allocate memory to is elements to store string.
for(int i=0;i<8;i++)
{
is[i] = malloc(20) //Assuming each array can hold only 20 chars including null character.
scanf("%s",is[i]);
}
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 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().
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.