Can somebody help me...!!!! This is terrible!!!

This is a discussion on Can somebody help me...!!!! This is terrible!!! within the C Programming forums, part of the General Programming Boards category; Arggg........I've stucked on this problem for 2 weeks.....my question is: I'm supposed to use calloc() or malloc() to create a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    32

    Unhappy Can somebody help me...!!!! This is terrible!!!

    Arggg........I've stucked on this problem for 2 weeks.....my question is:

    I'm supposed to use calloc() or malloc() to create a dynamic data structure (a.k.a: Dynamic 2D Array).

    I'm able to allocate memory sucessfully to create a 2D array, but my problem is: How do I insert values into the empty array which I created using calloc() or malloc()....??


    If I were to read the data from a file and place the data into the Dynamic 2D array......ermmm...how to do it..??


    I'm new in C, so hopefully anyone of you who see my post, please kindly give me your advice....and don't leave me alone....All help will be GREATLY appreciated.......Thanks!!!








    Assist Me!

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdlib.h>
    #define N 5 // Number of array elements
    
    int main(void){
      char *array = malloc(sizeof(char) * N);
      if(array){
        // insert data into array here
    
      }
      
      return 0;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well if you've done the job properly (which you didn't show us), then you simply do
    Code:
    int **twod;
    // some malloc calls (which you've done)
    twod[0][0] = 1;  // set the first element of the array
    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.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    *phew*
    I thought I did something wrong for a second there.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    32
    Oh...I'm sorry that I didi not show my codes because I thought that will be very messy......

    these are my codes......

    hey...wait.....what's wrong.....I can't post my codes here..!!!!
    They say please use code tags to format your code properly when I clicked "preview"....



    Explanation: I used calloc() to create a dynamic 2-D array first. Then, I'm supposed to get the data from file: Filename.data and place the data accordingly into the 2-D array.

    Hence, I used fgets() to get the first row of data first. Then, use strtok() to get the values from the row of data one by one and intend to place them into the 2-D array.

    But my major problem is: my table is declared in float and when I use strtok(), the datatype of the datas extracted from the original dataset become "char" datatype.

    Hence cannot be placed into the 2-D array anymore....

    My concern is: How to convert the datas back to float first b4 assigning them into the 2-D array?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by AssistMe
    hey...wait.....what's wrong.....I can't post my codes here..!!!!
    They say please use code tags to format your code properly when I clicked "preview"....
    << !! Posting Code? Read this First !! >>

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Most newbies code is messy, we are used to seeing that. You should post your code. People who come here and don't show their own efforts generally don't get much help.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. Help with section of code.
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2005, 05:38 PM
  3. My teeth are terrible
    By Displeased in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-17-2003, 08:38 AM
  4. Terrible Counting prog
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2002, 06:48 PM
  5. I Know A Secret!
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 08-14-2001, 05:20 PM

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