Thread: Why this dosen't work in C

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Why this dosen't work in C

    Following code can't be compiled as a C program & but when compiled as C++ program it works.

    I am experienced with C but quite to new to C++ & OOP, so please explain if you have some time.

    int *SomeArray=NULL,n;
    printf("Enter the number of elements in array : ");
    scanf("%d",&n);
    SomeArray = new int(n);

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    new is a c++ keyword.

    EDIT:
    Also... don't do this:

    int *SomeArray=NULL,n;

    do this:

    int *SomeArray=NULL;
    int n;

    Anyone reading your code will thank you later on. And you will thank yourself too.
    Last edited by Mario F.; 08-31-2006 at 07:48 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Yer, in C you gotta replace new with something like malloc() for dynamic memory.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> int *SomeArray=NULL,n;

    >> int *SomeArray=NULL;
    >> int n;

    I can see why Mario suggested you put it on different lines, but there isn't really a need. It's more for the sake of readability than anything. It's just that the pointer (*) indicator of SomeArray doesn't carry through to n, as I think you know. So, to distinguish between pointers, and non-pointers easily, new lines are often used.



    EDIT ::

    A more C++-ish implementation of your code Not color coded unfortunately

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
        int *SomeArray = NULL;
        int n = 0;
    
        cout<< "Enter the number of elements in the array: ";
        cin >> n
    
        SomeArray = new int[n];
    
        // Do stuff with the array
    
        delete []SomeArray;
    
        return 0;
    }
    Last edited by twomers; 08-31-2006 at 08:05 AM.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Thank you.

    Thank You, for giving C++ code.

    I tried to do following & compiled as C program but I am getting errors

    code> SomeArray = realloc(SomeArray,n);
    error> invalid conversion from `void*' to `int*'

    I get similar error if I do it using malloc

    (I use DevC++ IDE which uses gcc compiler)

    I googled but, everything I find is giving some errors, Anyone knows how to do it in C, or may be I should post it in C board ?
    Last edited by ruab; 08-31-2006 at 09:45 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're using a C++ compiler to compile C code. Save the file as a .c file. If that still doesn't work, you may have to cast the *alloc call. BTW, realloc doesn't do what you want. Try malloc().
    Code:
    SomeArray = malloc(sizeof(int) * n);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    malloc error

    I tried
    Code:
    SomeArray = malloc(sizeof(int) * n);
    but it's giving same error as above. When it's saved with .c extension.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    SomeArray has to be a pointer, not an array.

    Try including <stdlib.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you sure it's the same error, and not int to int* ?
    Did you include stdlib.h ?
    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but it's giving same error as above. When it's saved with .c extension.
    If you're getting the same pointer conversion error then you're still compiling as C++. C allows conversions to and from void*, but C++ requires a cast when converting from void*.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. Why dosen't this work
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 08-16-2002, 08:18 PM
  5. sigh... why dosent this work?
    By jon_nc17 in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 08:30 PM