Is this correct???way of doing??

This is a discussion on Is this correct???way of doing?? within the C Programming forums, part of the General Programming Boards category; Hello, Please anybody here can explain me whether i am doing this correctly or not?i want to create 2 dimesional ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Is this correct???way of doing??

    Hello,

    Please anybody here can explain me whether i am doing this correctly or not?i want to create 2 dimesional array of strings with double pointer. Here is the code kindly explain am i doing it fine or is there any mistake in it?


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <alloc.h>
    
    int main(void)
    {
    char **a,i;
    clrscr();
    
    a=(char** )malloc(sizeof(char)*10);
    
    for(i=0;i<10;i++)
    {
    	a[i]=(char* )malloc(sizeof(char)*4);
    	strcpy(a[i],"abc");
    	printf("&#37;s\n",a[i]);
    }
    getch();
    return 0;
    }
    Last edited by chottachatri; 02-11-2008 at 03:18 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    you should not cast malloc in C - see FAQ

    your sizeof is wrong - and you have a memory overrun

    your indentation also suffers

    Code:
    a=malloc(sizeof(*a)*10);

    Code:
    	a[i]=malloc(sizeof(*(a[i])))*4);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I think the first problem is with this line:
    Code:
    a=(char** )malloc(sizeof(char)*10);
    You want an array of 10 strings, but you allocated space for 10 chars. You probably want to use sizeof(char*) instead.

    Besides that, I note that we typically include standard headers with angle brackets (<>) instead of double quotes (""). Also, malloc() is declared in <stdlib.h>, but you did not include that... nor did you free() what you malloc()ed, but perhaps it does not really matter for this example.

    Note that clrscr() and getch() are non-standard, so you either leave them out and use standard (near) equivalents, or include the relevant non-standard library headers. Oh, and you might want to indent your code better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok i got it! thanks

    but 1 more question when i dont type cast it by writing (char **) it's giving warning Non-portable Pointer Conversion why does that happen?
    i am using Borland Turbo C++ 3.0 IDE

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You're using an awfully old IDE. Try upgrading.
    Here are a few recommendations.
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    but 1 more question when i dont type cast it by writing (char **) it's giving warning Non-portable Pointer Conversion why does that happen?
    Could be due to an old compiler, but I am not sure as C is not my forte.

    I suggest testing with:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char **a;
        int i;
    
        a = malloc(sizeof(char*) * 10);
    
        for (i = 0; i < 10; i++)
        {
            a[i] = malloc(4);
            strcpy(a[i], "abc");
            printf("&#37;s\n", a[i]);
    
            free(a[i]);
        }
    
        free(a);
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok and my last question is what are the standard equivalents of clrscr() and getch()??? i have this doubt since many days

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    Ok i got it! thanks

    but 1 more question when i dont type cast it by writing (char **) it's giving warning Non-portable Pointer Conversion why does that happen?
    i am using Borland Turbo C++ 3.0 IDE
    Because Turbo C++ doesn't follow the C89 standard that says that malloc returns a void * and that void * can be cast into anything else.

    Or you are using the Turbo C compiler in C++ mode - in which case that's the rules of C++. In that case, change your settings in the IDE so that this file is compiled as C, not C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Or get a better IDE.
    As for getch, getchar is standard (AFAIK). But there's no easy substitute for clrscr. However, there is an entry in the FAQ for just that, so you can have a look.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Ok and my last question is what are the standard equivalents of clrscr() and getch()?
    You could read the cprogramming FAQ or the cpwiki FAQ for the equivalents.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

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