Refreshing pointer with integer and character [closed] - c

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).

Related

how to swap two character of a character array? in c [closed]

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
How can you replace two characters in a character array? For example:
charecter array : peter
Replace the two characters p and t and give the following output:
teper
its my try(it is wrong):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char*,char*);
main() {
int n,i,j;
char str[30][30];
printf("how many names?:");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("name %d?",i+1);
scanf("%s",str[i]);
}
for(i=0;i<n;i++) {
char ch1,ch2;
printf(" which letters of name %d?:",i+1);
scanf("%c%c",&ch1,&ch2);
swap(&ch1,&ch2);
printf("\n %s",str[i]);
}
}
void swap(char *a,char *b){
char temp;
temp=*a;
*a=*b;
*b=temp;
}
You can do something like this
// you can use the same logic in c++
#include <stdio.h>
void swap(char * a, char * b) {
char temp = *a;
*a = *b;
*b = temp;
}
int main() {
char str[] = "peter";
printf("before -> %s\n", str);
swap(&str[0], &str[2]); // swap p and t
printf("After -> %s\n", str);
}

How to use if condition in structure in c [closed]

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");
}
}

Structs and pointers in C [closed]

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().

new programmer here,whats wrong with this stack program? [closed]

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].

How to cast a void pointer to point more than one struct? [closed]

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 9 years ago.
Improve this question
Having Problems about how to access other structure membera with void pointer of other function??
typedef struct
{
char Buf[20];
char Str[20];
}Sample;
typedef struct
{
char Data[20];
int i;
} Test;
Void pointer structure
typedef struct
{
void *New;
int j;
} Datastruct;
int main()
{
//i am confused with first line
Datastruct->New = &Sample;
strcpy((( sample*)Datastruct->New )->Buf,"adam");
printf(" Datastruct->New->Buf");
Datastruct->New = &Test;
strcpy((( Test*)Datastruct->New)->Data,"Eve");
printf("Datastruct->New->Data");
return 0;
}
please let me know how to access members of other structures via void pointers
The compiler is also confused about first line; you can't take the address of a type. As for following void pointers, you've got the right idea: cast it to the type of pointer you wish to treat it as.
Here is a fixed version which actually compiles and works without errors:
#include <string.h>
#include <stdio.h>
typedef struct {
char Buf[20];
char Str[20];
} Sample;
typedef struct {
char Data[20];
int i;
} Test;
typedef struct {
void *New;
int j;
} Datastruct;
int main() {
Datastruct d;
Sample s;
d.New = &s;
strcpy(((Sample*)d.New )->Buf,"adam");
printf("Datastruct->New->Buf\n");
Test t;
d.New = &t;
strcpy(((Test*)d.New)->Data,"Eve");
printf("Datastruct->New->Data\n");
return 0;
}
In your original you were confusing -> with . and types (e.g. Datastruct) with variables of that type.

Resources