Specify number of rows in an array??

This is a discussion on Specify number of rows in an array?? within the C Programming forums, part of the General Programming Boards category; Hi guys, first time posting here so I just want to thank anyone in advance for any help that I ...

  1. #1
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25

    Specify number of rows in an array??

    Hi guys, first time posting here so I just want to thank anyone in advance for any help that I might receive. I wish I had known about this forum earlier. Anyway, let me get to it. I am to write a program that lets the user specify the number of rows to be used in the array. More specifically:

    Main will make the following function calls:

    1. The first function will allow the user to specify the number of names in the list to be sorted. (Maximum 16 names can be in the list)

    That is from the lab assignment that my instructor has given me. There is a lot more to the program, but this is the first step, and I am having trouble with it.

    Here is my code so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //// PROTOTYPE DECLARATIONS ////
    int **buildTable (void);
    
    int main (void) //// BEGIN MAIN 
    {
    	int **buildTable (void); // <-Call to buildTable Function
    	
    	return 0;
    }
    
    
    int **buildTable (void) //// BEGIN buildTable Function
    {
    	int **table;
    	int rowNum, row;
    
    	printf("Enter the number of rows in the table: \n");
    	scanf("%d", &rowNum);
    	table = (int **)calloc(rowNum +1, sizeof(int*));
    	if(!table) return NULL;
    
    	table[row] = NULL;
    	return table;
    }
    When I compile it, I get no errors or warnings, but it does not print anything to the screen. What am I doing wrong here?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > int **buildTable (void); // <-Call to buildTable Function
    Nope, this is just another prototype

    Try
    int **myTable = buildTable(); // <-Call to buildTable Function

    > table = (int **)calloc(rowNum +1, sizeof(int*));
    Don't cast malloc (or calloc or realloc) in C.
    This is in the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25
    Wow, thanks for the fast response and the link. That helped me understand a little better. However, when i run the program now, it prints to the screen great, but it crashes when i input a value and press enter. Shouldn't the program just sit there because there is nothing going on? Or is it crashing because it doesn't know what to do next?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > table[row] = NULL;
    Probably because row is uninitialised, and you've just trashed some random memory location.

    > printf("Enter the number of rows in the table: \n");
    Don't you also need the number of columns as well?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25
    ahhh.. forgot to initialize..

    i added --> for (row = 0; row < rowNum; row++) <-- and it seems to work fine now.

    Thank you so much for your help. now onto the rest of the program for me. lets hope I can get it done.

    oh, and i don't have to specify the number of columns. I just have to ask the user how many rows he/she wants in the array.

    The first part of the assignment reads:

    Write a program that creates an array of strings, where the number of rows is specified by the user. The program reads the data from input file and sorts the array by the last name.

    Main will make the following function calls:

    1. The first function will allow the user to specify the number of names in the list to be sorted. (Maximum 16 names can be in the list)

    It will also obtain space in the heap for the array of strings. The array will have user defined number of elements. (Each string in the array will be 40 characters)

    From here I am guessing my next step would be to ask the user how many names he/she wants sorted and do some memory allocation to get space for the strings. I am going to work on it, but I am sure I will be back with some more questions.

    Thanks again for your help.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    If it's strings, you'll be wanting char** then, not int**
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. Replies: 8
    Last Post: 02-22-2009, 04:17 PM
  3. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 08:37 AM
  4. User defined 2-D array problem
    By Turtal in forum C Programming
    Replies: 5
    Last Post: 12-15-2007, 12:23 AM
  5. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM

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