Sponsored Content
Top Forums Programming segfault in pointer to string program Post 302555879 by zius_oram on Friday 16th of September 2011 02:14:57 AM
Old 09-16-2011
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[20]) why this function gives me segfault Smilieand the other code executes comfortably though both code uses same pointer arithmetic.

Code:
#include<stdio.h>
#include<string.h>
void reverse(char*);
int
main()
{
 char str[20];

 scanf("%s",str);
 printf("original string is %s :\n",str);
 reverse(str);
 printf("reverse string is %s  :",str);
 return 0;
}

void reverse(char* s)
{
 int n,mark,temp;
 n = mark = strlen(s);

 for(;s < (s + n/2);++s)
    {
      temp = *s;
      *s = *(s + (--mark));
      *(s + mark) = temp;
    }

 return;
}

while the code below executes happily

Code:
void my_strrev(char* begin){
    char temp;
    char* end;
    end = begin + strlen(begin)-1;

    while(end>begin){
        temp = *end;
        *end = *begin;
        *begin = temp;
        end--;
        begin++;
    } 
}

main(){
    char string[20];
    scanf("%s",string);
    my_strrev(string);
    printf("%s", string);
}

 

10 More Discussions You Might Find Interesting

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

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

3. Programming

id3lib SEGFAULT

Hello everyone, I'm writing a program using the id3lib unfortunately I've encountered with memory issue that cause segmentation fault. I tried to rerun and analyze the program with valgrind but it doesn't point me anywhere. I really stuck on this one. Valgrind output: ==14716== Invalid read of... (2 Replies)
Discussion started by: errb
2 Replies

4. Programming

C++ program is crashing on re-assigning const static member variable using an int pointer

Hi, Can any one tell me why my following program is crashing? #include <iostream> using namespace std; class CA { public: const static int i; }; const int CA::i = 10; int main() { int* pi = const_cast<int*>(&CA::i); *pi = 9; cout << CA::i << endl; } (6 Replies)
Discussion started by: royalibrahim
6 Replies

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

6. Programming

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... (6 Replies)
Discussion started by: raihan004
6 Replies

7. Programming

String array iteration causing segfault

I am populating an array of string and print it. But it going in infinite loop and causing segfault. char Name = { "yahoo", "rediff", "facebook", NULL }; main(int argc, char* argv) { int j = 0; ... (7 Replies)
Discussion started by: rupeshkp728
7 Replies

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

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

10. Programming

Segfault When Parsing Delimiters In C

Another project, another bump in the road and another chance to learn. I've been trying to open gzipped files and parse data from them and hit a snag. I have data in gzips with a place followed by an ip or ip range sort of like this: Some place:x.x.x.x-x.x.x.x I was able to modify some code... (6 Replies)
Discussion started by: Azrael
6 Replies
STRDUP(3)						   BSD Library Functions Manual 						 STRDUP(3)

NAME
strdup, strndup -- save a copy of a string LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * strdup(const char *str); char * strndup(const char *str, size_t len); DESCRIPTION
The strdup() function allocates sufficient memory for a copy of the string str, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned. The strndup() function copies at most len characters from the string str always NUL terminating the copied string. EXAMPLES
The following will point p to an allocated area of memory containing the nul-terminated string "foobar": char *p; if ((p = strdup("foobar")) == NULL) { fprintf(stderr, "Out of memory. "); exit(1); } ERRORS
The strdup() function may fail and set the external variable errno for any of the errors specified for the library function malloc(3). SEE ALSO
free(3), malloc(3), strcpy(3), strlen(3) STANDARDS
The strdup() function conforms to IEEE Std 1003.1-2001 (``POSIX.1''). HISTORY
The strdup() function first appeared in 4.4BSD. The strndup() function was added in NetBSD 4.0. BSD
January 28, 2009 BSD
All times are GMT -4. The time now is 02:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy