Thread: Printing a char.

  1. #16
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Quote Originally Posted by itsme86
    Well, you're lucky it works because str is pointing to a random memory address. You haven't allocated any memory for it. Try declaring str as an array instead of a pointer.
    While I was reading this.. I figured because I'm not allocating any memory on it, I don't have to put a pointer on it. So without the pointer it also works .

    My code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
          char str;
          printf("Name: ");
          scanf("%s", &str);
          printf("Hello %s\n", &str); 
          system("pause");
          return 0;
    }
    Jst.

  2. #17
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    That is because, array and pointers are treated differently. When arrays are declared with their size, a memory of that size if pre allocated at run time. Pointers on the other hand do not have any memory allocated at run time. They point to no where unless they are told to point at some variables address or some memory is allocated using malloc() for them to point at.

    The reason, jst you got the winxp error is because of core dump. When you read the name into str variable , it attempted writing into some other programs memory space, a space not owned by you. This is not allowed. Hence your program crashed. This is a programmer mistake. Compiler will not give any warning as there is no problem with the syntax anywhere. Its programmer's responsibilty to malloc the memory to pointer before storing something in it. Following should be the modified version of your program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
          char *str;
          printf("Name: ");
           str=(char *) malloc (10 * sizeof(char)); /*allocated 10 bytes of memory to store the name . This step is a MUST.*/
          scanf("%s", &str);
          printf("Hello %s\n", &str); 
          system("pause");
          return 0;
    }
    Last edited by nkhambal; 04-17-2005 at 12:47 PM.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    I've never worked with the maloc function before, but I guess it's time that I should...

    Jst.

  4. #19
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    While I was reading this.. I figured because I'm not allocating any memory on it, I don't have to put a pointer on it. So without the pointer it also works .
    There is a fundamental mistake with the code you have in this post. You are expecting user to enter the name as string. However, you are just reading the entire string into a single character. It will just store the first character of your name. Not the entire name.

    Read my last post. It tell you how to handle this problem with pointers.

  5. #20
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Quote Originally Posted by nkhambal
    However, you are just reading the entire string into a single character. It will just store the first character of your name. Not the entire name.
    Well, the last piece of code I posted did actually print my whole name or any other text I used for input.


    I don't know allot about these pointers yet, but when I declare the "%s" at a printf or scanf line to get a string, I don't have to dereference a variable I just have to retrieve the original value with the & pointer and then the printf function will print the string. I could be totally wrong, it's just a small theory .

    Jst.

  6. #21
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by ocy
    Code:
    ...
          printf("Name: ");
          scanf("%s", &str);
          printf("Hello %s", str); 
    ...
    Notice the difference? You'd missed something out, something very important.

    The scanf function needs to know where exactly to store your input, in memory. Since, there's a missing '&', so the function didn't know where. A mistake learners usually make, so, bare that in mind the next time you code, ok.
    Well, the arguments you pass to scanf must always be pointers, since str is a pointer there is no need to use &.
    Just a little correction

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #22
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    I guess you were just lucky.

    anyway, in C, strings are nothing but array of chars. i.e.

    Code:
    char name[10];
    when passing this array to printf() and scanf() requires you to pass the base address of the array without & operator. i.e.

    Code:
    scanf ("%s",name);
    printf("Name: %s\n", name);
    Another important thing to remember when dealing with strings in C is that, it is MUST to terminate all the strings with NULL character or ' \0 '. If this is not done then your prgram may crash at runtime, with string operating functions trying to read or write passed the mamory location allocated to it. When you allocate the memory to store a string, its always adviced to allocate an extra byte than the required size. Some string operting functions such as strcpy(), strdup() and strcat() take care of terminating the new string with null character. Nevertheless you need to take care of allocating an extra byte for it.

  8. #23
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Quote Originally Posted by Micko
    Well, the arguments you pass to scanf must always be pointers, since str is a pointer there is no need to use &.
    Just a little correction

    - Micko
    But Jst, said that there's an error with the program, before the '&' was used. So, wouldn't it be neccessary to include a '&'? Anyway, I don't have a compiler to verify that now, so if the '&' makes no differences, what could be wrong with the original code(below), then?

    Code:
    #include <stdio.h>
    
    main()
    {
          char *str;
          printf("Name: ");
          scanf("%s", str);
          printf("Hello %s", str); 
          system("pause");
          return 0;
    }

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    what could be wrong with the original code(below), then?
    It was already mentioned. There's no space allocated for the input string.
    If you understand what you're doing, you're not learning anything.

  10. #25
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    I did some testing with the codes that I had and found out that it works untill I input to many characters, everything below eight chars and it will work fine everything else and it will stop working (winxp error).
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char name[10];
        printf("Name: ");
        scanf("%s", name);
        printf("Hallo %s\n", name);
        system("pause");
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *name;
        printf("Name: ");
        scanf("%s", &name);
        printf("Hallo %s\n", &name);
        system("pause");
        return 0;
    }
    These where the codes I used. I'm not sure about this but I think this is why it is a good thing to use the malloc() function, it will take care of the memory allocation in some way (wich I don't know yet, never used the function, but I will).

    Jst.


    <edit>
    I tested the program again a couple of times, and even used the example with the malloc function, but it still gives the winxp error at the return 0; part i think, because he does executes the system("pause"), but when I press a button it stops running.
    </edit>
    Last edited by Jst; 04-18-2005 at 12:31 PM.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *name;
        printf("Name: ");
        scanf("%s", &name);
        printf("Hallo %s\n", &name);
        system("pause");
        return 0;
    }
    Are you not paying attention on purpose now, or what?

    1) You don't have any space allocated to scanf into. You're just scanning to (were it actually right) a pointer which points some place randomly.
    Code:
        char *name;
    This points where exactly?

    2) Your scanf call is wrong, because with pointers, you don't need the "address-of" operator, because scanf already expects a pointer.
    Code:
        scanf("%s", &name);
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    How would you create it?

    Tnx
    Jst.

  13. #28
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by Jst
    How would you create it?

    Tnx
    Jst.
    well you could do this, "char name[100];" instead of "char *name;" or if you want you can leave it as a pointer and allocate memory to it like this "name = malloc(100);". If you choose to allocate the memory, remember to free it again like this "free(name)". Of course these example are if you want it to be 100 chars long.

  14. #29
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    K, tnx.. Well I sure have allot to learn about pointers, although I do know allot more about it before I started this topic.

    Jst.

  15. #30
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by nkhambal
    That is because, array and pointers are treated differently. When arrays are declared with their size, a memory of that size if pre allocated at run time. Pointers on the other hand do not have any memory allocated at run time. They point to no where unless they are told to point at some variables address or some memory is allocated using malloc() for them to point at.

    The reason, jst you got the winxp error is because of core dump. When you read the name into str variable , it attempted writing into some other programs memory space, a space not owned by you. This is not allowed. Hence your program crashed. This is a programmer mistake. Compiler will not give any warning as there is no problem with the syntax anywhere. Its programmer's responsibilty to malloc the memory to pointer before storing something in it. Following should be the modified version of your program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
          char *str;
          printf("Name: ");
           str=(char *) malloc (10 * sizeof(char)); /*allocated 10 bytes of memory to store the name . This step is a MUST.*/
          scanf("%s", &str);
          printf("Hello %s\n", &str); 
          system("pause");
          return 0;
    }

    Why are you using the 'address of operator' on str in the scanf function?. It should be scanf("%s", str); ...in your example, you're giving the scanf call a pointer to a pointer (scanf("%s", &str)) . That's not right. str already exposes the address it points to so there's no need for &str.

    xeddiex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM