Thread: array of characters ending after a space

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    array of characters ending after a space

    Hey all,
    I'm relatively new to C, although I'm very familiar with PHP, so I'm aware of the general constructs of C. However...I'm having a small problem with an array of characters in a struct. When scanf'ing to the struct the array is ending after any 'spaces' in the string. For example if I were to enter my full name, it would simply display "Richard".
    This struct / string is also a part of a linked list, so I'm not sure if this might be causing a problem?

    Heres the overall view of whats happening...
    Code:
    typedef struct T_Lecturer {
      char Name[25];
    } LecturerInfo;
    
    typedef struct T_PtrNode {
      LecturerInfo Lecturer;
      struct T_PtrNode *Next;
    } LecturerNode;
    
    int main() {
      LecturerNode *NewEntry;
    
      NewEntry = malloc(sizeof(*NewEntry));
      if (NewEntry != NULL) {
          printf("Name: ");
          // John Smith
          scanf("%s", NewEntry->Lecturer.Name);
      }
      printf("%s", NewEntry->Lecturer.Name);
      // John
    }
    Thanks in advance,
    Richard.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    scanf will do that. You'd have to use fgets to get the whole string.

    Also, in this line :

    Code:
    NewEntry = malloc(sizeof(*NewEntry));
    Don't you mean to do :

    Code:
    NewEntry = malloc(sizeof(LecturerNode));
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Don't you mean to do :
    Why bother? Original variant should automatically ajust if the pointer type changes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    7
    Quote Originally Posted by Happy_Reaper View Post
    scanf will do that. You'd have to use fgets to get the whole string.
    I'll give it a go
    Quote Originally Posted by Happy_Reaper
    Also, in this line :
    Code:
    NewEntry = malloc(sizeof(*NewEntry));
    Don't you mean to do :
    Code:
    NewEntry = malloc(sizeof(LecturerNode));
    Hmm...would make more sense I guess

    -Update-
    fgets() works, but its now saving 25 bytes, including the '\n', making it very difficult to include it in a sentence (unless there is a string function to trim it?)

    With regards to altering the sizeof()...I tried LecturerNode and it ignored the 2nd input. I'll leave it as *NewEntry, it works, and doesn't cause any problems.
    Last edited by RichardH; 03-27-2007 at 09:18 AM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    fgets() works, but its now saving 25 bytes, including the '\n', making it very difficult to include it in a sentence (unless there is a string function to trim it?)
    A common way to get rid of the \n at the end of the input is to just turn the \n into a \0:
    Code:
    char str[25];
    
    fgets(str, sizeof(str), stdin);
    if(str[strlen(str)-1] == '\n')
      str[strlen(str)-1] = '\0';
    If you understand what you're doing, you're not learning anything.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    more common way is shown in the FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    7
    Quote Originally Posted by vart View Post
    more common way is shown in the FAQ
    Which thread are you referring to.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    7
    Awesome, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  2. read characters till white space
    By ashok449 in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 03:56 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. storing a pattern of characters into a 2D array
    By haroonie in forum C Programming
    Replies: 2
    Last Post: 04-20-2005, 05:19 AM