pointers seg faulting

This is a discussion on pointers seg faulting within the C Programming forums, part of the General Programming Boards category; Im trying to get some simple functions going but this keeps seg faulting on me. Ive tried it in a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    pointers seg faulting

    Im trying to get some simple functions going but this keeps seg faulting on me. Ive tried it in a few ways with no luck, tried IRC with no luck either(lower life forms).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main() {
     	char test[] = "123TEST456";
    	char *result;
    	
    	get_variable(test, result);
    	printf("\n\t%s\n", result); /* crashes here with: result, *result and &result */
    	free(result);
      	
    	return 0;
    	
    }
    
    int get_variable(char *src, char *dest) {
     	dest = malloc(100); /* just for now */
    	
    	copy(src, dest, 3, 7);
    	printf("get_variable dest = %s\n", dest);
    
    	return 0;
    }
    
    
    int copy(char *src, char *dest, int start, int end) { 
      	int x = start;
    	int y = 0;
    	
    	while( x < (end + 1) ) {
    		dest[y] = src[x];
    	
    		++y;
            	                ++x;
    	}
    	
    	dest[y] = (char)NULL; 
    
    	return 0;
    }
    man, talk about slow preview times...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Run this:
    Code:
    #include <stdio.h>
    
    void foo(char *p)
    {
        p = (char*)1;
    }//foo
    
    int main ()
    {
        char *p = (char*)0;
    
        printf("p before call: %d\n", p);
        foo(p);
        printf("p after call: %d\n", p);
    
        return 0; 
    }//main
    Do the same with the value of result before and after the call to get_variable.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2009, 05:02 PM
  2. Seg faults. structs. pointers.
    By ominub in forum C Programming
    Replies: 12
    Last Post: 05-03-2009, 08:04 PM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  4. seg faulting
    By Xyruul in forum C Programming
    Replies: 14
    Last Post: 04-18-2007, 02:44 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21