Thread: Some inquiries about C..

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Some inquiries about C..

    Hi all,

    My knowledge in java is more than C, and during the time I learn C, I found some issues that are very simple in java and if I wanted to do it in C I might spend time more that what I expect. One of the things is dealing with array in C, I need to specify the length of the array as a parameter, or initialize the array, or using vector...etc.

    What is the purpose of not having arr.length() in C?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays aren't "objects" like they are in Java. Thus, they do not have functions attached to them like Java objects do.

    However, for arrays declared in the current scope, you can use sizeof to find out its size. A simple bit of math from there will give you the number of elements.

    Plus, when they wrote C, they figured if you're not smart enough to keep track of how long your array is, then your program deserves to crash.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What is the purpose of not having arr.length() in C?
    Use a vector, and use C++ if you want such creature comforts.

    As well as being 30 years older than Java, C is also much lower level.
    Which means you have to do pretty much everything yourself.

    C was first developed on a machine with a total of 32K of memory. When was the last time you wrote a program smaller than this, never mind having an OS, Compiler and editor there at the same time?
    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.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    C is very close to the system. You can directly interface memory, and do lots of system stuff. C is also minimalist, so you get very few built-in functions. Arrays are treated not as objects, but as a string of continuous bytes in RAM. That's as basic as you can get. If you want to use the array length, you do it yourself. The compiler isn't going to put in what it THINKS you need; it will only but in the bare minimals. This is, in fact, one of Cs advantages over other languages.

    Treating everything as objects introduces a lot of overhead into the code, as well as runtime. C is built from the ground up to be efficient. That's how it is, kid, deal with it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If your going from Java, it's easier to learn C++, because it's object oriented. C++ vectors have a size method.

    The only potencial advantages C has over C++ is 1: no runtime type identification, and 2: no exeption handling. However many C++ compilers allow you to exclude those features anyway.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by Moony
    What is the purpose of not having arr.length() in C?
    Funny you would mention that, because the other day I was working on a Graphics engine in java and I was trying to increase performance by getting rid of the unused images files so as to lighten the buffer before rendering, and I found myself exclaiming :

    Darn, what is the purpose of Java not having a static void free(Object o) method ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed