Thread: pointer problem

  1. #1
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26

    pointer problem

    hi guys,

    can someone please tell me why this code isn't working?:
    Code:
    #include <windows.h>
    #include <stdio.h>
    int main(void)
    {
        char *run;
        printf("\nCommand:>");
        scanf(run);
        WinExec(run,SW_SHOW);
        return 0;
    }
    i'm not so good with pointers yet, i bet it'll be something simple, but i can't get it to work....
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You haven't allocated any space. Your pointer points at some random spot in memory. You're supposed to make a pointer point at something. That's why it's called a pointer. Furthermore, you're using scanf wrong.

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

  3. #3
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26
    well, i've used the pointer just like:
    Code:
    int main (int argc, char *argv[])
    at least, that's what i think, but how should i do allocate space?
    and should scanf() be used like
    Code:
    scanf("%s",pointer);
    ?
    cause when i use scanf() like that i get a memory error...
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    you get a memory error because the pointer doesnt point to anything like quzah said.

    Code:
      #include <stdio.h>
       
       int main(void)
       {
         int a, *a_ptr;  // 'a' is an int, a_ptr is a pointer to an int
         a_ptr = &a;    // a_ptr 'points to' the address of 'a'
       
         scanf("%d",a_ptr);
         printf("%d",a);
       
         return 0;
       }
    Last edited by sand_man; 08-06-2004 at 07:54 AM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your second attempt at scanf() is correct. You only need to change your definition of a to fix the memory error: char a[100];

    100 is a number I pulled out of a hat. The danger is if the user inputs a string that's more than 100 characters long scanf() will end up storing the string in your character array even though there isn't enough room for all the characters, writing to memory that isn't reserved for it. This is called a buffer overflow.
    If you understand what you're doing, you're not learning anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lepricaun
    well, i've used the pointer just like:
    Code:
    int main (int argc, char *argv[])
    at least, that's what i think, but how should i do allocate space?
    That's because argv is automaticly pointing at something (theoreticly*) when you start your program, because it stores the command line parameters. In effect argv, would be similar to you doing:
    Code:
    int main( void )
    {
        char *s = "pointing to this string literal";
    
        return 0;
    }
    Here, you're setting your pointer to point to a string literal. This is basicly what happens when you run your program and use the argv pointers. They're automaticly set up to point to any command line parameters you invoked when running your program, including, usually*, the program name.

    *I believe Thantos or Salem stated an example where argc was zero, and as such, the first argv[0] was null.

    As for how you allocate memory, look up malloc, realloc and free (for freeing when done).

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

  7. #7
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26
    thanks guys, this helps a lot
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    4
    Hi there! Heres an example

    Code:
    #include <stdio.h>
    
    int main()
    {
            char *n;
            n = (char *)malloc(10 * sizeof(char));
    
            printf("Enter name:");
            scanf("%s", n);
            printf("Hello! %s", n);
    
            return 0;
    }

  9. #9
    Quote Originally Posted by lonewarrior
    Hi there! Heres an example

    Code:
    #include <stdio.h>
    
    int main()
    {
            char *n;
            n = (char *)malloc(10 * sizeof(char));
    
            printf("Enter name:");
            scanf("%s", n);
            printf("Hello! %s", n);
    
            return 0;
    }
    • [Bad practice] Missing <stdlib.h>
    • [Design] 'n' is a strange name for a string...
    • [Undefined Behaviour] Useless cast (actually, masking the lack of prototype for malloc())
    • [Bad practice] Useless sizeof (char) (actually, 1 by-definition)
    • [Undefined Behaviour] No check of the value returned by malloc()
    • [Design] Why using allocation with a fixed value where a static array would suffice?
    • [Undefined Behaviour] Missing fflush(stdout) after an unterminated line.
    • [Cosmetic] Missing space after ":"
    • [Undefined Behaviour] Missing check of the value returned by scanf()
    • [Security issue] Missing a width formatter with scanf() "%9s".
    • [Design] Missing handling of pending '\n' after scanf(). fgets() would have been a better choice for many reasons.
    • [Undefined Behaviour] Missing fflush(stdout) or '\n' after an unterminated line.
    • [Memory leak] Missing free() of the allocated block.

    Do you think you are qualified to answer on this board? Please read and learn. We all have done that before posting, there is no shame to have.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    Quote Originally Posted by Emmanuel Delaha
    • [Undefined Behaviour] Useless cast (actually, masking the lack of prototype for malloc())
    Don't mean to start a flame war on this, but I believe that casting pointers returned by malloc(), as long as it is properly declared, helps document your code. From an "utilitarian" point of view, yes, it's completely useless, but I guess you could argue the same for comments, for using '\0' instead of 0, and so on

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > "as long as it is properly declared"
    See, there's the rub
    If you cast it, you have no way to tell whether it is properly declared or not because you effectively turn off all typechecking.

    If you don't declare it (by not including stdlib.h), then the compiler will
    a) implicitly declare int malloc(); the first time you try and use it.
    b) then complain that you're now trying to convert an int to a pointer.
    Any programmer who is remotely awake will instantly know what to do to fix the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
      int bufsize=256;
      char *buffer=malloc(bufsize);
      while(1)
      {
        printf("\nCommand:>");
        WinExec(buffer,SW_SHOW);
        scanf("%s",buffer);
      }    
      return 0;
    }
    this is what i've got from your replies, and it works :P
    so thanks!

    only one other question:
    now the scanf() only reads one word, meaning: when i submit a command with parameters, like cd \ i actually submit 2 commands, cd and \.
    how can i solve this problem, isn't a <space> just a character?
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't freeing the memory you allocate, and you still aren't checking to make sure that malloc isn't failing. Be sure and do both.

    Beyond that, consider reading the FAQ entry on reading input.

    Oh, and you forgot [code] tags ...

    ...in your SIG!

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

  14. #14
    noob lepricaun's Avatar
    Join Date
    Jul 2004
    Posts
    26
    i'll read the faq,
    hope in the faq also is explained how to free memory and how to check if malloc() didn't fail, otherwise i hope google will give the answer....

    oh yeah, b.t.w., i've wanted to change my sig, anyway, so don't worry, it'll be ggne/done in a flash
    The path of access leads to the tower of wisdom...
    __________________________________________________ ___________________

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
            const char buf[17]="Hello everybody!";
    	printf("%s\n",buf);
            return 0;
    }
    

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The man page for malloc gives you this information. Here's an excerpt:
    RETURN VALUES

    For calloc() and malloc(), the value returned is a pointer
    to the allocated memory, which is suitably aligned for any
    kind of variable, or NULL if the request fails.
    Thus, all you have to do is test the pointer to see if it's NULL or not.

    On an aside, I sure wish [quote] kept indenting like [code] does...

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM