Thread: Is this correct???way of doing??

  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 04:18 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed