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]);
}
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 6 years ago.
Improve this question
I want to convert my char[] array to char* string in C programing language. Here is what I did. But I can't reach the solution . Please help me. Here is my code:
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk((char*) charfoo[j]);
}
}
I think you mean the following
fnk( &charfoo[i]);
Take into account that it would be better to write the loop the following way
for( int j=0; charfoo[j]; j++ ) {
fnk( &charfoo[j]);
}
Also function gets is unsafe and is not supported any more by the C Standard. Instead use function fgets For example
fgets( charfoo, sizeof( charfoo ), stdin );
In this case the loop can look like
for( int j=0; charfoo[j] != '\0' && charfoo[j] != '\n'; j++ ) {
fnk( &charfoo[j]);
}
If you want to output just one character in the function then the function should be defined like
void fnk(char chr){
printf("%c",chr);
}
and called like
fnk( charfoo[j]);
If we switch from gets() to fgets() as Vlad from Moscow suggests, then the return value from fgets() is of the same type as the argument to fnk() so we can just pass the result along:
#include <stdio.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
fnk(fgets(charfoo, sizeof(charfoo), stdin));
return 0;
}
The next step in sophistication would be to save the result of fgets() to a char * pointer and test if it's NULL or not before passing it along (or not) to fnk().
If you want an actual char * copy of the array charfoo[] to exist in memory and not simply pass off char[] arrays as char * pointers, you could do the following which throws in lots of error checking too:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fnk(char *string) {
printf("%s", string);
}
int main() {
char charfoo[100];
char *input = fgets(charfoo, sizeof(charfoo), stdin);
if (input != NULL) {
char *string = strdup(input);
if (string != NULL) {
fnk(string);
free(string);
}
}
return 0;
}
But in all cases, it seems like you're making things more difficult than they need to be.
Calling fnk with i would just be the same call 50 time -- assuming you want to use j instead the code would look like this
#include <stdio.h>
void fnk(char *chr){
printf("%s",chr);
}
int main(){
char charfoo[100];
int i=50;
gets(charfoo);
for(int j=0;j<i;j++){
fnk(charfoo + j);
}
}
be aware that gets could cause buffer overflow if the input is longer than the charfoo (i.e. 99 bytes + newline + null) -- but that is a different story
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 need to get result from __VA_ARGS to a function, and from there I need to pass string of each argument to a 2d character array.
If you are under C99, you can use compound literals
#include <stdio.h>
#define MACRO(...) func( \
sizeof((char *[]){__VA_ARGS__}) / sizeof(char *), \
(char *[]){__VA_ARGS__} \
)
void func(size_t n, char **p)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%s\n", p[i]);
}
}
int main(void)
{
MACRO("abc", "def", "ghi");
return 0;
}
Notice that __VA_ARGS__ are evaluated twice in order to get the number of elements using sizeof, as an alternative you can send NULL as last parameter (sentinel):
#include <stdio.h>
#define macro(...) func((char *[]){__VA_ARGS__, NULL})
void func(char **p)
{
while (*p != NULL) {
printf("%s\n", *p);
p++;
}
}
int main(void)
{
macro("abc", "def", "ghi");
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
whats wrong here?
I want to check if the char in char array islower, so if it is it should be changed in uppercase.
#include <stdio.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s",&arr);
for(int i=0;i<sizeof(arr);i++){
if(int islower(arr[i])){
arr[i] = toupper(arr[i]);
}
}
printf("%s",arr);
return 0;
}
To measure the length of a string properly, use strlen, not sizeof
for(int i=0;i<strlen(arr);i++){ // Don't use sizeof on this line
Here's a simpler version:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s", arr);
for(int i=0;i<strlen(arr);i++){
arr[i] = toupper(arr[i]);
}
printf("%s",arr);
return 0;
}
Or even:
#include <stdio.h>
#include <ctype.h>
int main(void) {
char arr[100];
scanf("%s", arr);
for(char* c=arr; *c=toupper(*c); ++c) ;
printf("%s",arr);
return 0;
}
You are missing an include #include <ctype.h>
Also you don't need your if statement. toupper takes care of that internally (if you really want to keep islower remove the int in your if statement).
Add the header that declares islower and toupper.
#include <ctype.h>
In addiition,
if(int islower(arr[i])){
is not right. Remove the int.
if(islower(arr[i])){
whats wrong here?
The line: if(int islower(arr[i])){ fails compile for bad expression.
Change to: if(islower(arr[i])){
And in this line in your code may be looking beyond where it should:
for(int i=0;i<sizeof(arr);i++){
as you may be looking at space past the string terminator:
|s|t|r|i|n|g|\0|<unknown contents here, part of your legal memory, but are not part of the string>
it should be:
int len = strlen(arr);
for(int i=0;i<len;i++){
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].