Why It's Giving Me Segmentation Fault? - c

here's the Code:
#include <stdio.h>
#include <string.h>
int main()
{
char C1[6] = "Hello";
char* C2 = C1;
char C21 = C1[1];
printf("\n\nC2 in String : %s\n", C2+1 );
printf("\n\nC2 address : %d\n", C2+1);
printf("\n example de C2+1 : %s", C21);
}
and here's the output
C2 in String : ello
C2 address : 6422273
Segmentation fault

You declared C21 as a char not char pointer. Either change the declaration to:
char * C21 = &C1[1];
or else change %s to %c in the third printf.
If you turn warnings on the compiler will give you a message that lets you work this out for yourself. All -Wall to your command line if you are using gcc.

Related

process returned -1073741819 <0xC0000005>

i am a beginner to coding and learning c language as my first step.
when i use string function the program returns with a value without showing output. using minGW as compiler
i tried to add string.h header folder
string
from below follows code
''''
#include <stdio.h>
#include <conio.h>
int main()
{
/*
int strlen(string);
*/
char name = { 's','i','l','a','m','\0' };
int length;
length = strlen(name);
printf("the length of %s is %d\n", name, length);
getch();
return 0;
}
'''
code ends here
expected to to print length of the char name but it crashes
as in build log
"Process terminated with status -1073741819 "
in build messages
warning: passing argument 1 of 'strlen' makes pointer from integer without a cast [-Wint-conversion]|
note: expected 'const char *' but argument is of type 'char'|
thanking you for looking into
You declare name as a char yet you treat it like an array. To declare name as a char array, use:
char name[] = { 's','i','l','a','m','\0' };
Also since you reference the function strlen(), you must add the header file:
#include <string.h>
Enjoy:
#include <stdio.h>
#include <string.h>
int main()
{
// single char can't store that string
// so we have to declare an array of chars
char name[] = {'s', 'i', 'l', 'a', 'm', '\0'};
// also we can use string literal syntax:
// char name[] = "silam";
int length = strlen(name);
printf("the length of %s is %d\n", name, length);
getc(stdin);
// i don't have <conio.h> so i call 'getc' instead of 'getch'
return 0;
}

How to enter char* in C?

Its a simple input that I want to make for char*. Why is this not working?
It throws me an exception that I can't resolve..
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
char* GetCharSeq()
{
char *s = (char*)malloc(100);
scanf_s("%s", s);
return s;
}
int main()
{
char* charseq;
charseq = GetCharSeq();
return 0;
}
You have undefined behavior in your code. You have it because you provide to few arguments to the scanf_s function.
For every string argument, you need to provide not only the destination string but also the size of the string. So change your call to
scanf_s("%s", s, 100);
modify your code
char* GetCharSeq()
{
char *s = (char*)malloc(100);
gets(s);
return s;
}
This will work.

How can I fix a pointer error in C?

I am starting to learn about pointers in C.
How can I fix the error that I have in function x()?
This is the error:
Error: a value of type "char" cannot be assigned to an entity of type "char *".
This is the full source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void x(char **d, char s[]) {
d = s[0]; // here i have the problem
}
void main() {
char *p = NULL;
x(&p, "abc");
}
In function x() you pass d which is a char ** (a pointer to a string pointer), and char s[] (an array of char, passed as similarly to a pointer to char).
So in the line:
d = s[0];
s[0] is a char, whereas char **d is a pointer to a pointer to char. These are different, and the compiler is saying you cannot assign from one to the other.
However, did your compiler really warn you as follows?
Error: a value of type "char" cannot be assigned to an entity of type "char *"
Given the code sample, it should have said char ** at the end.
I think what you are trying to make x do is copy the address of the string passed as the second argument into the first pointer. That would be:
void x(char **d, char *s)
{
*d = s;
}
That makes p in the caller point to the constant xyz string but does not copy the contents.
If the idea was to copy the string's contents:
void x(char **d, char *s)
{
*d = strdup(s);
}
and ensure you remember to free() the return value in main(), as well as adding #include <string.h> at the top.
A more proper way would be to use strcpy:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void x(char **d) {
*d = malloc(4 * sizeof(char));
strcpy(*d, "abc");
}
int main() {
char *p;
x(&p);
printf("%s", p);
free(p);
return 0;
}
Outputs: abc

Invalid operands to binary * ( have 'char *' and 'char **' ) error

I have write simple program as follows.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* alphabate[]={
(char *)"xyz",
(char *)"abc",
(char *)"pqr",
NULL
};
void main()
{
char **pp;
for( pp=alphabate; *pp; pp++ )
{
printf("\n alphabate member %s" *pp);
}
}
but when i compile it on my Linux machine then it show following error at printf() statement.
test.c:19: error: Invalid operands to binary * ( have 'char *' and 'char **' )
Any idea to resolve it?
printf("\n alphabate member %s" *pp);
you are missing a comma before *pp
printf("\n alphabate member %s" *pp);
should be
printf("\n alphabate member %s", *pp);
Use a , in printf after the printf("\n alphabate member %s",*pp);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* alphabate[]={
(char *)"xyz",
(char *)"abc",
(char *)"pqr",
NULL
};
int main()
{
char **pp;
for( pp=alphabate; *pp; pp++ )
{
printf("\n alphabate member %s",*pp);
}
return 0;
}
There's a comma missing after the string in printf. It must be
printf("\n alphabate member %s", *pp);
With your code, the * is interpreted as multiplication.
Casting string literals to char * is redundant. Also the signature of main in C should be either:
int main(void)
or
int main(int argc, char *argv[])
You are missing a comma in the printf function. It should be
printf("\n alphabate member %s", *pp);

Unable to retrieve data from array of character inside structure

I am using below code in one file
file 1
//structure is global
struct abc
{
char var;
char *a[5];
}*p;
struct abc q;
int main()
{
char t[] = "sample"
p = &q;
p->a[0] = &t[0];
p->var = 10;
printf("var = %d, string = %s\n", p->var, p->a[0]);
func();
exit(0);
}
But if I try to access the structure member (a[]) in func() that is in another file I don't get the data that is assigned in another file (above).
file2
int fucn()
{
char var1;
var1 = p->var;
printf("var1 = %d\n", var1);
//since i am unable to copy p->a[0] to some other string i am trying to print the contents of p->a[0].
printf("a = %s\n", p->a[0]);
}
program crashes executing the second printf but I can print the content of p->var which is assigned in some other file.
Something like below is what you need.
An include file.
prompt> cat foo.h
struct abc {
char var;
char *a[5];
};
extern struct abc *p;
main function
prompt> cat main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "foo.h"
struct abc q, *p;
extern int func();
int
main()
{
p = &q;
/*
* Using strcpy after allocating memory.
*/
p->a[0] = malloc(strlen("zero") + 1);
strcpy(p->a[0], "zero");
/*
* strdup is equivalent to malloc and strcpy
*/
p->a[1] = strdup("one");
p->a[2] = "two";
p->a[3] = "three";
p->a[4] = "four"
p->var = 10;
printf("main var = %d, string = %s %s %s %s %s\n",
p->var, p->a[0], p->a[1], p->a[2], p->a[3], p->a[4]);
func();
return(0);
}
func function
prompt> cat func.c
#include <stdio.h>
#include "foo.h"
int
func()
{
int r;
r = printf("func var = %d, string = %s %s %s %s %s\n",
p->var, p->a[0], p->a[1], p->a[2], p->a[3], p->a[4]);
return(r);
}
Compile and run
prompt> gcc mainc.c func.c
prompt> a.out
main var = 10, string = zero one two three four
func var = 10, string = zero one two three four

Resources