Thread: Wrong returned pointer

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    Wrong returned pointer

    Hello,

    I give you here an sample of my code. The main trick is that a function return a pointer obtained from argument. The sample given here is just an array but in my entire program it is a structure containing integer, pointer ...
    I warn that code bellow work but if you translate it and using a structure instead string allocated it becomes not work.

    The issues is that in my real program the function retfunc() return a wrong pointer . It advance to 1 point (or something close to 1). The string allocated inside the structure advance and some integer that indicate a position of a cursor text advance to 1 point. An integer that indicate at what string position to start the printing advance to 1 point. Due to this issues all data are scrambled when viewing it on screen.

    Could some one tell me why the function return a wrong pointer ?

    I am under linux.


    Code:
    #include <stdio.h>
    
    char *data, *ext_point;
    // In my real code data is a structure containing pointer and integer
    
    char *retfunc(char *arg_point)
    {
     // The real program return a structure not a char
     return(arg_point);
    }
    
    int main(void)
    {
     data= (char*) malloc(100);
     strcpy(data, "This is a test");
     
     ext_point=data;
     
     ext_point=retfunc(ext_point);
     printf("ext_point:&#37;s\n", ext_point);
     
     return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not pass in the structure in an argument as a pointer then edit it from there...

    eg:

    Code:
    typedef struct foo_t {
    	unsigned short int eg;
    } foo;
    
    void bar(foo * s)
    {
    	s->eg = 10;
    	return;
    }
    
    int main(void)
    {
    	foo t;
    	bar(t);
    	// t.eg is now 10...
    	
    	return 0;
    }
    Also don't cast malloc! (read the FAQ)... If you want a answer to your original question, please ask it properly (in correct English) and maybe supply the real code? Also look at non-standard strdup()

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zacs7 View Post
    If you want a answer to your original question, please ask it properly (in correct English) and maybe supply the real code?
    It's quite possible that English is not their native language.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by quzah View Post
    It's quite possible that English is not their native language.


    Quzah.
    I understand that, but they should at least supply the original code so we could piece together the question...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. what's wrong with my pointer?
    By iluvmyafboys in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 02:08 PM