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

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

Related

storing string in array [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 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]);
}

convert binary to decimal with linked list 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
Hello I have a problem with my code its shows this error: "Segmentation fault: 11" here is my code i'm trying to make a program that convert binary linked list to decimal using lists in C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct cellule{
int b;
struct cellule* Next;
}Cellule;
typedef Cellule* liste;
int BinToEntier(liste a){
int n;
int j=0;
liste p=NULL;
for (p=a; p->Next!=NULL; p=p->Next) {
n+=(p->b)*pow(2,j);
j++;
}
return n;
}
int main() {
liste a=NULL;
liste p;
a= (liste) malloc(sizeof(Cellule));
p=a;
for (int i = 0; i < 4; i++) {
puts("enter b");
scanf("%i", &(p->b));
p=p->Next;
}
printf("%i\n", BinToEntier(a));
return 0;
}
In:
a= (liste) malloc(sizeof(Cellule));
a is not initialized to zero, yet in your loop you do a p=a;...p=p->Next. This will access undefined memory potentially causing a seg fault. (Note that scanf("%i", &(p->b)); can also cause the seg fault.)
Also in BinToEntier you forget to initialize n.
Your for loop in the function main() is causing the segmentation fault. Simply put, you are not allocating space for each node(i.e. Cellule) of the list, but allocating just the first element. Additionally, but almost as equally importantly, your assignment of the last node you append to the tail of the list is erroneous.
Consider switching to this usage given below.
int main() {
liste p=NULL;
liste *tp = &p;
for (int i = 0; i < 4; i++) {
*tp = malloc(sizeof(Cellule));
puts("enter b");
scanf("%i", &((*tp)->b));
*tp=&((*tp)->Next);
}
*tp = NULL;
printf("%i\n", BinToEntier(p));
return 0;
}
The code given above uses a variable tp, which is a pointer to a pointer to a Cellule. (i.e. list node) In effect, we update tp to show the "Next" attribute of the last Cellule in the list, and update it as we add new nodes to the list p.

Refreshing pointer with integer and character [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 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).

Characters not showing up [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 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.

have a conflicting types error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
when compiling my c file i get this error
i can't seem to get my types correct for this program, how would I go about fixing this problem
I put up my .h file as well as my .c file
error
example4.c:35: error: conflicting types for ‘h’
example4.h:8: error: previous declaration of ‘h’ was here
example4.h code
typedef struct{
int x;
char s[10];
}Record;
void f(Record *r);
void g(Record r);
void h(const Record r);
example4.c code
#include <stdio.h>
#include <string.h>
#include "example4.h"
int main()
{
Record value , *ptr;
ptr = &value;
value.x = 1;
strcpy(value.s, "XYZ");
f(ptr);
printf("\nValue of x %d", ptr -> x);
printf("\nValue of s %s", ptr->s);
return 0;
}
void f(Record *r)
{
r->x *= 10;
(*r).s[0] = 'A';
}
void g(Record r)
{
r.x *= 100;
r.s[0] = 'B';
}
void h(Record *r)
{
r->x *= 1000;
r->s[0] = 'C';
}
Your header file declares void h(const Record r);
while your source file declares void h(Record *r)
You fixed the source file, but forgot to fix your header, when you were trying to apply the answer I gave you to this question.

Resources