Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C - Use a Pointer array in parameter function (strange result) (http://www.go4expert.com/forums/c-pointer-array-parameter-function-t18252/)

lennin.caro 29Jun2009 20:23

C - Use a Pointer array in parameter function (strange result)
 
hi a have this code

Code:

///////////////////
#include <stdio.h>
FILE * open_file(char *file);
int read_file(FILE *p_File,char *parameter[]);
int close_file(FILE *p_File);
int main (){
    FILE *p_file;
    int ret;
    int i=0;
    char *p_array[100];
    p_file=open_file("/tmp/prb.txt");
    ret=read_file(p_file,p_array);
    for(i;i<=1;i++){
        printf ("value in main : %s \n", *(p_array+i));
    }
    close_file(p_file);
    return 0;
}
//read a file and fill a array of pointer
int read_file(FILE *p_File, char *p_Line[]){
    int i = 0;
    char p_use[100];
    if (p_File == NULL){
        return;
    }
    while(fgets(p_use,100,p_File)) {
        *(p_Line + i) = p_use;
        printf ("value in read_file : %s \n",*(p_Line + i));
        i++;
    }
    return 0;
}
//open a file
FILE * open_file(char *p_Arch){
    FILE *p_File;
    p_File=fopen(p_Arch,"r");
    if (p_File == NULL)
    {
        fprintf(stderr, "%s \n", "Erro open file");
        return NULL;
    }
    return p_File;
}
//close a file
int close_file(FILE *p_File){
    if (fclose (p_File) != 0)
    {
        fprintf(stderr, "%s \n", "Error close file");
        return 1;
    }
    return 0;
}
/////////////////////////////////

the problem is this, if a run this code the result is

value in read_file : host = 127.0.0.1
value in read_file : port 5502
value in read_file : user = usr
value in read_file : pass = password
value in read_file : aaaa
value in read_file : bbb
value in read_file : .
value in read_file :
value in read_file : ccccccc
value in main : ccccccc
value in main : ccccccc

and the cuestion is this...

For that when I execute it like appears here the result of "value in main" is the same for several time and not show me the result like "value in read_file"

and when comment the line

printf ("value in read_file : %s \n",*(p_Line + i));

the result is

value in main : ������h&��`��[H�� (��8[��
value in main : ������h&��`��[H�� (��8[��

pd: i new in C

SaswatPadhi 30Jun2009 00:34

Re: C - Use a Pointer array in parameter function (strange result)
 
Welcome to the forum lennin.caro :)

I will definitely try to help you, but I cannot exactly understand what your question is.
Can you be a bit more clear ??


All times are GMT +5.5. The time now is 05:43.