How i use pointer as a string in c programing?


 
Thread Tools Search this Thread
Top Forums Programming How i use pointer as a string in c programing?
# 1  
Old 09-30-2012
Error 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.

Code:
#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?
# 2  
Old 09-30-2012
The core dump is occurring because you are not declaring any memory space to store the string that you are inputting.

Try the following:
Code:
#include <stdio.h>

char buffer[100];

int main()
{
    char *s = buffer;

    gets(s);
    puts(s);
}

# 3  
Old 09-30-2012
but without declaring arry its work on windows how and why?
# 4  
Old 09-30-2012
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

Code:
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().
# 5  
Old 10-01-2012
Quote:
Originally Posted by raihan004
but without declaring arry its work on windows how and why?
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.

Last edited by Corona688; 10-01-2012 at 12:39 PM..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 10-01-2012
check out with this code...

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

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 10-01-2012 at 05:35 PM..
# 7  
Old 10-01-2012
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.

Code:
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);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

String pointer does not work

Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted. #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "kseq.h" // STEP 1: declare the type of file... (5 Replies)
Discussion started by: yifangt
5 Replies

2. Programming

Scanf() string pointer problem

I have a problem with scanf() for string pointer as member of a struct. #include <stdio.h> #include <stdlib.h> struct Student { int studentNumber; int phoneNumber; char *studentName; //line 7 // char studentName; //line 8 }; int... (10 Replies)
Discussion started by: yifangt
10 Replies

3. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

4. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

5. IP Networking

Netork programing

Hello experts, please help me as i want to learn the networking concepts in details , as i come know Unix network programming by Richard Stevens volume 1,2 is good please any of you downloaded the Free PDF version of it please direct m e as i want to download these books or the pdf form of it,... (1 Reply)
Discussion started by: vin_pll
1 Replies

6. Shell Programming and Scripting

shell programing...

Hi... i need to write a shell script wich shows the full name and station of every logged user in the system. pls help! (1 Reply)
Discussion started by: relu89
1 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. Programming

String and pointer problem

i am having a string like " X1 " ---> string lenght is 30 I have stored this to a chararry . ref so here ref = " X1 " now i trim the left space by my function . Si the string now becomes "X1 " ---> string lenght is 15... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

9. Shell Programming and Scripting

Awk Programing (need help)

plx help to solve these problems?? 1. Create a HERE document which will edit multiple files in the same directory, using the ed editor. I give you 3 original files: file1.c , file2.c , file3.c, download them and change each string "stdio.h" to "STDIO.H" in these files. Note: when execute the... (1 Reply)
Discussion started by: SoCalledEngr
1 Replies

10. HP-UX

crontab programing

How can program at crontab dayly each 30 minut (2 Replies)
Discussion started by: petroleo
2 Replies
Login or Register to Ask a Question