Thread: Problem using scanf with array

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Problem using scanf with array

    Hello everyone!
    I'm a beginner and I was facing a problem here.

    I am trying to use scanf to input filenames as strings to different indexes in the array char **filenames.

    For example:

    filenames[0] = "lol1.txt";
    filenames[1] = "lol2.txt";
    filenames[2] = "lol3.txt";
    filenames[3] = "lol4.txt";
    filenames[4] = "lol5.txt";

    But my code doesn't seem to work:

    Code:
    	int totalfiles = 0;
    
    	printf ("Enter the total number of files to be merged:");
    	scanf ("%d", &totalfiles);
    
    	char **filenames;
    	filenames = (char**)calloc(sizeof(char**), totalfiles);
    
    
    	// I tried using the code below but it messes up the program
    
    	int n = 0;
    
    	while ( n < totalfiles )
    	{
    		printf ("File number %d:", n+1);
    		scanf ("%s", filenames[n]); // I'm assuming the problem is in this line
    		n++;
    	}
    Any ideas on what I'm doing wrong here?

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How about malloc()'ing storage for the arrays that each element of filenames[] points to.
    Note that the arguments to calloc() are switched around, so better fix 'em.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Oh..., you are doing all sorts or errors. First of all, you need to learn the difference between arrays and pointers, second you need to understand the difference between multidimmensional arrays and pointer to pointer, these are not equal, AND have totaly different purposes.

    Give it an other try, repost if you can't get it right, and I'll give some additional leeds.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    itCbitC: I does not matter if changes the paramters, the calloc statement is still wrong. The sizeof(char**) will yield 4, which is the size of the pointer and not the sizeof an element in the multi dimmensional array. If he was supposed to something close to correct, it would be something like (not compiled and tested):

    Lets say that max file name is 10 chars.

    Code:
    char **fileNames = NULL;
    
    fileNames = (char**)calloc(totalfiles, sizeof(char) * 10);
    But still there is a lot of errors in the code.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is wrong. You are allocating 10 chars, but store it in a pointer to pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Quote Originally Posted by Elysia View Post
    This is wrong. You are allocating 10 chars, but store it in a pointer to pointer.
    No, its not, you are allocating totalFiles, which is the input, times 10 which is the size of one element the array times the size of the element type.

    However, you are right about the pointer to pointer.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by xcubic33 View Post
    Hello everyone!
    I'm a beginner and I was facing a problem here.

    I am trying to use scanf to input filenames as strings to different indexes in the array char **filenames.

    For example:

    filenames[0] = "lol1.txt";
    filenames[1] = "lol2.txt";
    filenames[2] = "lol3.txt";
    filenames[3] = "lol4.txt";
    filenames[4] = "lol5.txt";

    But my code doesn't seem to work:

    Code:
    	int totalfiles = 0;
    
    	printf ("Enter the total number of files to be merged:");
    	scanf ("%d", &totalfiles);
    
    	char **filenames;
    	filenames = (char**)calloc(sizeof(char**), totalfiles);
    
    
    	// I tried using the code below but it messes up the program
    
    	int n = 0;
    
    	while ( n < totalfiles )
    	{
    		printf ("File number %d:", n+1);
    		scanf ("%s", filenames[n]); // I'm assuming the problem is in this line
    		n++;
    	}
    Any ideas on what I'm doing wrong here?

    Thanks!
    I also tried your problem but with a slightly different approach. Here are my steps.
    1. Declared an array of char pointers(char *). Assuming the total files as 5. char *filenames[5].
    2. Entered the total number of files.
    3. Ran a while loop till "totalfiles" just like you did.
    4. Allocated memory to the array using malloc.
    5. Rest of the part was like your while loop.
    But I'm curious to know how we can do it using pointer to pointer to char. I tried to do that way but always resulted in segfault.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Woah...thanks for the overwhelming response everyone!

    I'll take everyone's post into consideration, read the links, do some more studying myself and then get back here!

    I am actually trying to make a merge and split program in C as part of my university's course project. This is my first semester and we just started learning C.

    Thanks again!

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    I think I solved the problem:

    Code:
    void main ()
    {
    
    	clrscr();
    	int totalfiles = 0;
    
    	printf ("Enter the total number of files to be merged:");
    	scanf ("%d", &totalfiles);
    
    	char filenames[10][10];
    
    	int i;
    
    	for (i=0; i < totalfiles; i++)
    	{
    	printf ("Enter file number %d:",i+1);
    	scanf ("%s", filenames[i]);
    	}
    
    	printf ("\n\n");
    
    	int p = 0;
    
    	printf ("Analysing the following %d files\n", totalfiles);
    	while ( p < totalfiles )
    	{
    		printf ("%s\n", filenames[p]);
    		p++;
    	}

    As you can see, I used a simple two dimensional string array instead:
    char filenames[10][10];


    Everything seems to work now.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    As you have just started with C, it would be beneficial if you go according to the standards which says that main returns int.
    Code:
    int main(void)
    {
    your code;
    return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also beware of scanf: SourceForge.net: Scanf woes - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by BEN10 View Post
    But I'm curious to know how we can do it using pointer to pointer to char. I tried to do that way but always resulted in segfault.
    Starting out malloc() storage for the char** variable.
    This creates an array of char* pointers with the char** pointing at the base.
    Finally, malloc() storage for an array of char - one per char* pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM