How i use pointer as a string in c programing?

I'm newbie learner. My all friend use windows just only me use linux.
so i can't solve any problem by myself. i need a solution.
how can i use pointer as a string.

#include<string.h>
#include<stdio.h>
int main()
{
char *s='\0';
gets(s);
puts(s);
return 0;
}

This code work on windows but not work in my linux mint.
i got output like this
Segmentation fault (core dumped)

how i coding any program like this on work?

The core dump is occurring because you are not declaring any memory space to store the string that you are inputting.

Try the following:

#include <stdio.h>

char buffer[100];

int main()
{
    char *s = buffer;

    gets(s);
    puts(s);
}

but without declaring arry its work on windows how and why?

Windows C - [C++, C#] does not follow any external standards. So I cannot say why it "works" on windows. It is completely incorrect and should have failed.

Windows and linux both have good compilers. You can set options to warn about bad things in your code on either windows or linux. With linux

gcc -Wall  myprog myprog.c

will help you find problems. A segmentation fault is sort of the ultimate warning from linux that you did something really bad.

I am guessing that, later on, the program would fail on Windows when it ran more code: like you added several more lines of code and added calls to library functions like printf().

By complete coincidence, nothing more. If this was a debug build, a release build might crash, or vice versa. You are using undefined behavior -- undefined doesn't mean "will crash", undefined means "unpredictable".

Windows will let you get away with very stupid things -- temporarily. It allowed me to delete my video surface sixty times per second and not crash then and there. It still crashed, but far later, in mysterious ways which took days to debug, because not crashing immediately meant I had no clue where the bug was.

Using undefined memory means you're wrecking something, somewhere. You might be overwriting your stack pointer, wrecking your ability to make function calls. You might be overwriting something in heap space, ruining heap pointers and causing weird malloc() crashes later. You might be in data space and overwriting your global variables. This is not safe, stable, or sane. Memory does not work that way. Use a buffer.

1 Like

check out with this code...

#include<stdio.h>
#include<string.h>
void print(char *);
int main()
{
	char s[10];
	scanf("%s",s);
	print(s);
	return 0;
}	
void print(char *s1)
{
	printf("%s",s1);
	
}

it will work in any platform..

scanf has several problems for this.

1) You cannot tell scanf how big your buffer is. If someone types in 11 characters, it will overflow and do weird things to the memory beyond your buffer, whatever it may be.

2) scanf can leave information in the buffer, especially when you use things besides %s, which ends up being really annoying, having to fflush() all the time.

What I would do instead is use fgets(). It reads entire lines at a time, into a buffer of known size.

fgets(s, 10, stdin);

...and if you need to use scanf to parse integers and the like, use sscanf on that buffer. sscanf(line, "%d %d %d", &inta, &intb, &intc);