1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer Questions

Discussion in 'C' started by techme, May 31, 2010.

  1. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    1, a pointer (here "pointer") points to a data stream to a file is located.
    2, A variable (in this case "m)", then how many numbers there are.
    3 m to retire pointer back to the beginning point also.
    4 data output.
    But the code does not work as-planned. The is the first value only.
    Who can help me?
    Thanks

    Code:
    # include <stdio.h> 
    # Include <stdlib.h> 
    int main () 
    ( 
    char * pointer, 
    unsigned char m = 0; 
    FILE * file, 
    file = fopen ("data.txt", "rt") 
    while ((fscanf (file, "% d", pointer))! = EOF) 
    ( 
    pointer + +; 
    m + +; 
    ) 
    fclose (file); 
    
    pointer = pointer - m, / / Pointer to withdraw 
    
    while ((* pointer)! = NULL) 
    ( 
    printf ("% d \ t", * pointer) / / output data 
    pointer + +; 
    ) 
    )
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    pointer has no memory!

    should you allocate memory with malloc, since otherwise the pointer showing nirvana! nirvana and the data write is not good ;)
     
  3. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Did you "pointer = (char *) malloc (sizeof (char ));"?
    I have used, but that does not help.
    Can you give me the full code?
    Thanks
     
  4. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    malloc (sizeof (char)) provides you space for a char ... you need but probably more ... So it must malloc (n * sizeof (char)) where n is the number is called.
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    char instead of int Why? You read% d end of ints.
    Have you ever thought of using a tuned array?
    Why do you break from the output at 0? Why do you write NULL instead of 0?

    sizeof (char) is always a way, and should never be explicitly written out.

    The return value of malloc should not cast according to some experts, are also, in any case, it's at least useless.
     
  6. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I have rewritten the program. Seems to be better. The only problem is I get a unendlose loop. So the "\ 0" is
    definitely not true, but what is the right one? What is the difference btwn NULL, EOF and "\ 0"?
    Code:
    # include <stdio.h> 
    # Include <stdlib.h> 
    int main () 
    ( 
    int * pointer, 
    unsigned char m = 0; 
    FILE * file, 
    int character 
    file = fopen ("data.txt", "rt") 
    while ((fscanf (file, "% d", & sign) ) = EOF) 
    ( 
    (pointer + m) = sign *, 
    m + +; 
    ) 
    fclose (file); 
    
    m = 0; 
    while ((pointer + m)! = '\ 0') 
    ( 
    printf ("% d \ t" , * (pointer + m)) 
    m + +; 
    ) 
    )
     
  7. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    hand still has no memory: (

    Hoer times n bit Bashar.

    NULL is the same as (void *) 0 - and is usually used to say that this pointer is invalid '. "

    EOF - End Of File, indicates that the file is zueende.

    "\ 0" is a string (char *) with the following contents, '\ 0' "\ 0 'well what do you think is more
    "\ 0 'which has the value 0 and indicates the end of the strings!
     

Share This Page