Thread: whats worng with my code???

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    whats worng with my code???

    Code:
    first my english not that good so try to understand 
    
    
    so im trying to do a very simple task 
    i need to write a function that gets an empty array of strings and its size and initialize it (for exemple "names={"dani", "gabi", "bobi"}, names is array of size 3 and each one hold a string that the user puts) 
    so first the user need to put the size then i allocate the array and then i trying to get strings for the array each one for diffrent address
    
    
    now for some reason its work greatly for the rest of the array exept for the one in the first index (arr[0])
    i understand that because the "ENTER" is a char it gets it for the first string (when the user puts the size...)
    
    
    but i dont want it to be like this and im trying to fix it for hours and im hopeless 
    
    
    please help me with that im geting crazy i might punch my comuter because of rage!!
    
    
    Code:
    #define _CRT_SECURE_NO_WARNINGS#define SIZE 21
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    enum { FAIL = 0, SUCCESS = 1 };
    
    
    int init_arr(char ***arr, int *size);
    
    
    int main()
    {
        int i, sizes = 0;
        char **course = NULL;
    
    
        init_arr(&course, &sizes);
    
    
        for (i = 0; i< sizes; i++)
            puts(course[i]);
    
    
        free(course);
        return 0;
    }
    
    
    int init_arr(char ***arr, int *size)
    {
        int i;
        char* c;
    
    
        printf("Enter the Number of--->:");
        scanf("%d", size);
    
    
        *arr = (char**)malloc((*size) * sizeof(char*));
        if ((*arr) == NULL)
        {
            printf("Error");
            return FAIL;
        }
    
    
        for (i = 0; i < (*size); i++)
        {
            c = (char*)calloc(SIZE, sizeof(char));
            if (c == NULL)
            {
                printf("Error");
                return FAIL;
            }
            printf("Enter Name %d-->:", i);
    
    
    
    
            fgets(c, SIZE, stdin);
    
    
            (*arr)[i] = c;
        }
    
    
    
    
        return SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    From FAQ > Flush the input buffer - Cprogramming.com

    I suggest trying
    Code:
    int   ch;
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    After the scanf.

    I also suggest using a C Compiler instead of a C++ Compiler to test and write C code.
    Edit: I am guessing you are using a C++ Compiler because you cast the malloc which is normally done under C++; but, not under C.
    FAQ > Casting malloc - Cprogramming.com

    Tim S.
    Last edited by stahta01; 05-14-2017 at 01:42 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > free(course);
    Count how many times you call malloc/calloc.

    You need to free each course[i] before doing this.

    > init_arr(&course, &sizes);
    Why are you returning success/fail if you're just going to ignore it?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats the code for this , i need help
    By lalaguy97 in forum C Programming
    Replies: 3
    Last Post: 12-05-2015, 01:25 AM
  2. Replies: 8
    Last Post: 10-31-2011, 01:45 AM
  3. What am I doing worng?
    By curt22 in forum C++ Programming
    Replies: 1
    Last Post: 01-27-2010, 02:16 PM
  4. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 09:59 AM
  5. Whats going on with my code?
    By Junior89 in forum Windows Programming
    Replies: 1
    Last Post: 01-15-2006, 03:17 PM

Tags for this Thread