Scanf into a array of strings

This is a discussion on Scanf into a array of strings within the C Programming forums, part of the General Programming Boards category; Code: char *titles[10]; int main(void) { int i; char *temp; for(i = 0; i < 10; i++) { printf("Enter Team ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Scanf into a array of strings

    Code:
    char *titles[10];
    
    int main(void)
    {
        int i;
        char *temp;
        
        for(i = 0; i < 10; i++)
        {
            printf("Enter Team Name %d: ", i + 1);
            scanf("%s", &temp);
            strcpy(titles[i], temp);
        }
    
        for(i = 0; i < 10; i++)
            printf("%s", titles[i]);
    
    }
    I get a segfault with the scanf. How to I do this properly?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    You declared pointers, but you need to dynamically allocate memory for them to be assigned to.
    Look into "malloc()".

    If this is too difficult for now, you could always declare character arrays of a fixed size to read the strings into.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    My compiler gave a lot of warnings:

    Code:
    C:\Users\thismoment\Documents\c programming\test6\main.c||In function 'main':|
    C:\Users\thismoment\Documents\c programming\test6\main.c|10|warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|10|warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|11|warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|11|warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|11|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char **' [-Wformat]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|12|warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|12|warning: incompatible implicit declaration of built-in function 'strcpy' [enabled by default]|
    C:\Users\thismoment\Documents\c programming\test6\main.c|16|warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]|
    ||=== Build finished: 0 errors, 8 warnings (0 minutes, 0 seconds) ===|
    Adding #include <stdio.h> and <string.h> fixed up most of the warnings, leaving only this one:
    Code:
    C:\Users\thismoment\Documents\c programming\test6\main.c|11|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char **' [-Wformat]
    You have not allocated memory for the pointer *temp. Since temp is a pointer, &temp is a pointer to a pointer of type char( char **). So after you have allocated memory, use scanf("%s", temp). Don't need the & at the front.
    Last edited by cfanatic; 11-16-2012 at 04:52 PM. Reason: added mroe content
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Now the strcpy portion causes a segfault. Any ideas?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    Please post your updated code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with strings [using scanf()]
    By Luponius in forum C Programming
    Replies: 4
    Last Post: 04-26-2011, 10:27 AM
  2. Using scanf function with strings
    By laurenlb in forum C Programming
    Replies: 4
    Last Post: 03-24-2011, 10:02 PM
  3. Help with scanf and strings
    By trueman1991 in forum C Programming
    Replies: 2
    Last Post: 11-14-2009, 06:03 AM
  4. Strings in Scanf function
    By dcwang3 in forum C Programming
    Replies: 21
    Last Post: 09-11-2008, 03:47 AM
  5. scanf for strings?
    By pobri19 in forum C Programming
    Replies: 7
    Last Post: 05-31-2008, 03:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21