A rant about C with a question at the end. :P

C is okay. It's just really hard to understand.
So this is my rant about it. It starts out by listing where I've been in terms of programming and then lists my difficulties with C itself.
I discovered C many years ago. I'm not too sure how. All I know is that for many years I've wanted to know what this C thing was all about, but had no way or knowledge of how to. I didn't really know anybody before I got the 'net last year, so asking someone else was out of the question. Plus, I wouldnt've known what question(s) to ask.
As I became more proficient with the whole technology thing, I began to go to the local libraries nearby and use their free internet access to look stuff up. But I didn't get the idea to do any research about C, most likely because I didn't have the mental capacity to plan well and my access was once or twice weekly at best. Plus, the only computer I had at home was 66MHz and ran DOS, and I only knew BASIC around that time which I was comfortable and content with, so wasn't looking for a new language. I think I knew about assembly language but didn't pursue it if I did.
A couple of computer upgrades later, I finally got the 'net at home and began to see the convenience of being able to spontaneously look something up when I wanted to as good, rather than weird. Ideas gradually began to pop into my head that I'd run off and look up, and one day C became one of them. So I did some research on C and finally began to learn what it was: an incomprehensible mess of manually managed memory and low-level trickery. Since I knew a fair bit about assembly language now I decided C was worse than asm (and I had no chance of learning asm back then), and left it at that.
Being the BASIC coder I was (pun intended), I found the syntax and concepts over my head by several miles. But the fascination never died, and kept poking at me again and again. And I just had to keep forgetting about it.
I moved on from QuickBASIC, which I'd been using for just about everything, to FreeBASIC, which was in such a beta state when I tried it, I disliked it enough to move on and discovered PHP.
PHP has been called the BASIC of the 21st century, and I can understand why. There are typically at least 10 ways to do something whether in principle or method, multiple functions do the same or an equivalent task, and overall it just doesn't look like a good language. But beyond all that, PHP made sense to me. As I went along many of the things I expected to perform in a given way performed in said way, which was a huge confidence boost. The model it followed might have been convoluted, but something inside my head "clicked" when I saw PHP, so I liked it, since I could understand it easily.
But recently, I found myself hitting too many walls. I wanted to get into graphical software development, and none of the graphical extensions PHP had available would work for me. PHP-GTK did work but prevented PHP from outputting anything as an Apache module, without throwing a single error anywhere. PHP-QT wouldn't compile on my system. PHP-TK did compile, but both segfaulted when any tk_* function was called and also exhibited the Apache module issue.
So I decided then and there that I needed to switch languages.
The problem was, my current machine happens to be so slow that a GIMP operation that executes in 6 seconds on the lowest generation AMD CPU from 4-5 years ago will execute in 38 seconds on mine. I have no idea why; the machine is, after all, 2.66GHz.
Although PHP is pretty snappy on my system, I don't know how other languages would fare and don't really want to risk the experiment; I want to switch to a language I know will be as fast as, or faster than, PHP. So I decided that a compiled language would have to be it, since I regarded PHP as the fastest language I knew. I was probably wrong, but whatever.
So I set my mind to understanding C. I'd asked around online before, and someone took pity on me and explained the basics of C and I did my best to understand them. This was great for a couple of days, but didn't last all that long unfortunately. So I asked around again, dreading their responses, and with good reason too: most of the tutorials I read often left me boggled and flew over my head at one point or another, which didn't help.
But I got some dreaded tutorial links, and did my best to read them. It took me a couple of weeks to finally convince my brain to understand the point of a language that utilizes variables that doesn't contain any data, but instead references to other variables. I eventually grasped the why, but am still having quite a hard time understanding the how.
So then, after that initial hurdle, I moved onto other topics. Or rather tried to. I quickly discovered that C interconnects some of its issues so deeply that it's hard to learn one thing at a time, move on and learn the next thing - how I best learn. I don't create paths very well by reading information; I create those paths by staying away from information and letting it slowly process in my mind. But to actually remember it in the first place, it needs to make sense, and C doesn't make sense because things are so interconnected and... we come full circle.
To be honest, after about a fortnight of trying to understand pointers, I still don't understand them. I've read what is probably the best the web has to offer about pointers. Has it helped? Not really.
Either the documentation:
- is too terse and I have to slow down, risking losing interest in what I'm reading
- skips over vital points or assumes I know Pascal or some other language
- is hard to understand or is poorly written
- puts segments in the wrong order, so I don't understand everything something might depend on before I reach that something
...and in general I either give up on reading the documentation, give up on C or just cry. I've done the 3rd a few times, the 2nd quite a few times and the 1st pretty much as many times as I've read documentation.
So what are my issues with C?
Let me address the two I can think of right now:
Pointers aren't addressed "simply"; if I declare a pointer to int named x I have to use *x to access what x points to, rather than use *x to get x's location, like one does with non-pointer variables where & is used to get an address. It'd be nice if we used &x to get x's value if x wasn't a pointer and *x to get x's value if x was a pointer, rather than use this convoluted scheme.
Also, I've heard that it's a common misconception that C handles arrays natively. Elsewhere, I've heard that strings are merely arrays of char. I can understand the 2nd, but what do I do with the 1st?
I do understand that if I say printf("%d\n", x[1]) I'm effectively saying x++; printf("%d\n", x), but I don't understand how this fully works. For example, let us consider the following code:
char *filename = "<insert file here>";
int i;
struct stat filebuffer;
int status;
FILE *handle;
handle = fopen(filename, "r");
status = stat(filename, &filebuffer);
char *buffer = malloc(filebuffer.st_size);
for (i = 0; i < filebuffer.st_size; i++) buffer[i] = fgetc(handle);
printf("%s\n", buffer);
You'll of course notice the array notation. Do I understand why if I replace said notation with something to the effect of...
buffer++
buffer = fgetc(handle);
...the program segfaults? No. Likewise, I don't understand why the...
buffer++
*buffer = fgetc(handle);
...in the loop does work, printf("%s"...); causes segfaults and printf("%d"...); does not.
Another thing I found is that declaring a char *, filling it with data, and then using memcpy on said char *, the program works fine, but if I declare for example char x[1] = "hi"; the compiler will shout at me for not allocating enough arrays. Only while writing this did I realize that the 2nd element is for the null byte.
Suffice to say that C confuses me. To bits. Although I have written a 500 line program in it (579 to be exact). All said program does is display a message on the screen via Xlib, but I managed to figure out how to make a word wrapping engine using strsep and how to use XDrawPoint XDrawLine to not only create a nice UI but also let me define various UI "styles" which can be loaded at startup.
Said program uses a very big helping of "if it doesn't work stab it until it does", so not only do I not understand how a lot of it works, it probably wouldn't compile under anything except gcc. Which is from a theory perspective quite a problem, IMHO. Almost every 2nd variable is typecast to this or that type.
So put simply, every tutorial or introduction to C hasn't made a lot of sense to me. Maybe I learn slowly; I'm hoping it's that, because I can't see anything besides assembly language which would be faster than C. I plan to learn asm after I've mastered the very basics of C, but I may end up having to learn the other way around if I expect to get anywhere.
Like I said in the title, I have one actual question in this post. So here it is:
Are there any fun, easy reading tutorials out there that don't visually look like they came out of 1992 and read like http://poignantguide.net/ or http://learnyouahaskell.com/?
-dav7

dav7 wrote:Pointers aren't addressed "simply"; if I declare a pointer to int named x I have to use *x to access what x points to, rather than use *x to get x's location, like one does with non-pointer variables where & is used to get an address. It'd be nice if we used &x to get x's value if x wasn't a pointer and *x to get x's value if x was a pointer, rather than use this convoluted scheme.
Pointer syntax is extremely unfortunate. The best part:
char *a; // It's a pointer.
*a; // It's a character. Good move, guys.
Also, I've heard that it's a common misconception that C handles arrays natively. Elsewhere, I've heard that strings are merely arrays of char. I can understand the 2nd, but what do I do with the 1st?
Insofar as C has arrays, C strings are character arrays. C arrays are just a promise that the system won't mess within a certain set of memory addresses. Array syntax is a concise way to calculate and dereference a specific location in memory, relative to an address that you hope is the beginning of some memory you reserved. array[n] = *(array + n), right? Personally, I would have left out the subscript notation entirely. Regardless, any time you choose to use brackets, you can mentally substitute in an explicit addition and dereference there. You're saying, "Give me the number stored at the memory address I have just calculated, which I know by my science holds something I put there intentionally, and not garbage at all."
One more clarification with regard to pointer arithmetic is necessary: when you add 2 to an int*, the resulting address is actually 2*sizeof(int) bytes after your base address. Same for a double*: 2*sizeof(double).
I do understand that if I say printf("%d\n", x[1]) I'm effectively saying x++; printf("%d\n", x), but I don't understand how this fully works.
It's not the same! First of all, in example #2, you're passing a pointer to an int. In example #1, you're passing an integer, since [] dereferences pointers for you. Any time you use those brackets, it's as if you had typed an asterisk yourself. Second, in example #1 you're not messing with x. In example #2 you're incrementing x by four bytes (the likely size of one integer). Doesn't make a difference in this short example, but if x ever gets used again it will be very significant.
Another thing I found is that declaring a char *, filling it with data, and then using memcpy on said char *, the program works fine, but if I declare for example char x[1] = "hi"; the compiler will shout at me for not allocating enough arrays. Only while writing this did I realize that the 2nd element is for the null byte.
Looks like a counting problem. Arrays are indexed from 0, but sizes are specified from 1 on up. Your "char x[1]" has only index [0]. To hold a two-character string, you'll need at least size three (char x[3]), so that x[0]='h', x[1]='i', x[2]='\0', the null terminator.
Suffice to say that C confuses me. To bits.
Any time you get confused by pointers or arrays (which are the same thing), take a step back and think about what's happening in terms of memory addresses. Draw it out on paper if you think you're making a mistake. In some languages you can ignore low-level details like that and write perfectly fine programs, even though you won't be able to optimize them without knowing how your code maps onto the machine. But in C, everything that looks like a high-level feature is just shorthand for setting a few bytes to different numbers, and the abstraction is so leaky that you can't get by without understanding it. Types are just a way for the compiler to catch dumb mistakes (some people believe this is helpful) and to automate some math, like the pointer arithmetic above. In a running program there are no types, so when you're trying to figure out what a program is actually doing, you need to consider what the types are shorthand for.
If you're having trouble "thinking like a programmer", by which I mean sanity-checking your use of syntax and stepping through a program so you know what it does, you might want to get up to speed in a different language, or by working through a book on algorithms.
Last edited by pauldonnelly (2008-11-03 22:54:12)

Similar Messages

  • How to contact a live person about resetting my security questions. The link to send email doesn't work.

    I need to contact a live person about resetting my security questions.  The link to send an email, doesn't send an email even though the email address it's supposed to be sent to is verified and works. (It can receive other email).

    Hello DogTrainer63,
    Click on the first link below to find the number that you need to contact Apple to help sort you out. I have also provided an article that may help if you have not received that email to reset the questions. 
    Contact Apple for support and service
    http://support.apple.com/en-us/HT201232
    If you didn't receive your Apple ID verification or reset email
    http://support.apple.com/en-us/HT201455
    Regards,
    -Norm G. 

  • Questions for the end users in Building an application.

    Questions for the end users in Building an application.
    Hello,
    I am assigned a project in building a CF application. As far
    as the business requirements, I have an idea from the MIS people.
    However, I have a meeting with direct users and I have to ask
    questions to add to my requirements. I am still a beginner in this
    but I need to ask questions.
    Can anyone give me tips? what basic / important questions?

    For starters,
    DO:
    Ask them to discuss what they want to do with the application
    Try to understand their level of computer literacy
    Keep your conversation non-technical
    Continue to interact with the users during the design and
    development process
    Try to identify a "champion" with whom you can interface as the
    project progresses
    DON'T:
    Use technical terms or discuss technical issues
    Talk down to them
    Promise delivery dates until you have documented their
    requirements and they have approved them

  • HT5312 How can you fix a email with a .con at the end

    Well, everytime i try to reset my security questions, the password is sent to a email with a .con at the end. Does anyone know how to change the email, because it won't let me.

    You need to contact Apple to get the questions reset. Click here, phone them, and ask for the Account Security team, or fill out and submit this form.
    (94610)

  • Is there a way to rename multiple form field names with a "b" at the end?

    I have a two page form, both are identical.  I need to rename all the field names on page 2 with a "b" at the end of the file name so they dont conflict with that of the first page.  Is there any fast way of doing this without renaming each individual one at at time?  I have a LOT to do!

    It's not possible with JavaScript. The name property of a field is read-only.
    I would suggest making a template from the first page and then spawn a new page from it.
    If you do it from a script make sure you set the bRename property to true and the form fields will be automatically renamed, but you can't specify the name, it will be in following pattern:
    P<Page Number>.<Template Name>.<Original Field Name>

  • Attempting to download a wmv file. I keep getting the message: "You cannot save this document with extension ".wmv" at the end of the name. The required extension is ".webarchive" --if I save it this way, i just get a web address rather than the wmv

    Attempting to download a wmv file. I keep getting the message: "You cannot save this document with extension “.wmv” at the end of the name. The required extension is “.webarchive” --if I save it this way, i just get a web address rather than the wmv

    Find the URL for the WMV, paste it into Safari's address bar, and press the Option and Enter keys.
    (66873)

  • Everytime I save a file from chrome it wants to save it with a (1) at the end of the file name. It i

    Everytime I save a file from chrome it wants to save it with a (1) at the end of the file name. It is extremely frustrating since I save hundreds of pdf files every month.
    It is the first time opening the file so it shouldn't be reverting to adding a (1) every time.
    I have chrome Version 28.0.1500.95 m

    Chrome does not use Adobe Reader to view or save PDF files. It does it all itself.
    Similarly FireFox, though it can be told to use Adobe Reader to view PDF files, doesn't do so by default. And Adobe Reader has nothing to do with saving.

  • How can I best create a movie in flash ( .flv ) with a link in the end?

    I use iMovie and have QT pro. I want to make a movie with a link in the end of the video: " See more here". Clicking it ( on the video window) would open my website.
    Then I want publish it in flv. format, because some people can't use Quicktime.
    Yours truly my Friends forever,
    Antti

    AnttiS wrote:
    .. I want to make a movie with a link in the end of the video: " See more here". Clicking it ( on the video window) would open my website.
    I assume, this is easier done as an html-task.. like that:
    (example is a still, forum software doesn't allow embedding of 'videos')
    define the player as an playercode

  • HT201210 i want to update my iphone 3g but it keeps coming up with an error at the end, how do i go about solving this problem?

    hi all,
    im having lots of difficulties updating my iphone 3g. i have plugged it into itunes, it goes ahead with updating and restoring. it goes through all the process but right at the end it tells me it was unable to do it and that there was an error. my phone now cant be used as it keeps telling me to connect to itunes which i cant do as the phone needs restoring.
    please could you advise me on how to solve this problem?
    thankyou

    and error code or the exact msg would help

  • What is the last question at the end of auto-updates? "Allow" what? (I clicked on "Done" too fast.)

    Hi, I installed an auto-update today, lazily clicked on "Done" at the end without taking the time to read the last "pop-up" dialog box. I saw options about "allowing" something but I did not get a chance to read the question before it disappeared. What was that last question???

    replying to JS1111
    Now I only get one HKEYLOCALMACHINE\Software\
    QuicktimePlayerLib.QuicktimePlayerApp\CLSID.
    some folks have been having some success with pgfpdwife's technique in the following post:
    pgfpdwife: Re: Could not open key HKEYLOCALMACHINE\Software\Classic\Quicktime.Quicktime\
    note carefully that the technique involves a registry edit. be sure to make a backup of any keys you edit. if you're unfamiliar with your registry or registry editing, head to your XP help and support, do a search on registry, and read through the articles that come up.
    There are also some instructions on how to back up registry keys in the following document:
    Error 1406 or 1402 appears when you install iTunes or QuickTime for Windows

  • Search with a Star at the end

    Hi All,
    I'm making a parameter on the Customer name, and my client want to have an "intelligent search engine" like putting only the first letters of the name and having all the names starting with theses letters.
    For example, if i type "pa" i want that all the name starting with "pa" are displayed and not only the exact search.
    I'm looking for a kind of formula that put automatically a start at the end of my search.
    Somebody can help me for that or have another ideas ?
    Thanks
    ps: sorry for my poor english
    edit: in Crystal Report 2008 server edition
    Edited by: Crystal_sophie on Jun 1, 2009 11:07 AM

    you can make use of startswith or create your own logic.
    using crystal function startswith:
    create a string parameter "{?name starts with}" for input and put this formula for record selection
    {Customer.Customer Name} startswith {?name starts with}
    a simple logic could be like this:
    Left ({Customer.Customer Name}, len({?name starts with})) like {?name starts with}
    let me know if it works well.

  • I am fairly new to iPhone and noticed yesterday that up by the bluetooth icon there is a small circle with a point on the end and a lock in the middle.  I cannot find out what this is in my manual

    I am fairly new to the iPhone and noticed yesterday up by the Bluetooth icon a small circle that is 90% complete and has an arrow at the end and a small lock in the middle.  I have made no changes in my settings and cannot figure out what this is.  Any help would be appreciated. 
    Thanks

    Orientation lock.  See p. 13 of the iPhone User Guide.
    You can turn this On/Off with Control Center (swipe up from off the bottom of the screen).  See p. 30 of the user guide.

  • BIC tables with @[at]k1 in the end

    Hello gurus,
    I have a problem in customer's system - database analysis in transaction DB02 shows that following tables takes very much space: /BIC/B0000091@k0, /BIC/B0000091@k1, /BIC/B0000091@k2 and /BIC/B0000091@k3.
    I know that these are some kind of PSA tables, but they do not exist in transaction SE11. I tracked down the DataStore object for table /BIC/B0000091000 and deleted all the PSA contents for the object, but these tables with @k in the end are not deleted and keeps using the database space...
    Any ideas what are these tables and how to delete them?
    Thank you in advance!
    Br,
    Juris

    Solution
    There are two main steps you must perform:
    (1) fix the reason why the table drop fails and then
    (2) remove the shadow tables so processing can continue.
    To determine why the partition drop failed, we recommend checking:
    1. The SQL Server Error Log u2013 this will tell you if the transaction log ran out of space. The log can be seen via SQL Server tools or transactions ST04/DBACOCKPIT.
    2. The Blocking Lock Monitor u2013 as long as you have the monitor job active, this transaction will show if there is blocking occurring. This monitor is in transactions ST04/DBACOCKPIT.
    3. The Deadlock Monitor u2013 will show if there was a deadlock at the time of the partition drop. This monitor is in transactions ST04/DBACOCKPIT.
    After you have determined the root cause, you must correct it before removing the shadow tables. If the log ran out of space then you must move it to a drive with enough space or add another log file on a different drive with enough space. If the root cause is blocking or deadlocks then first search SAP Notes for known solutions or, secondly, open a customer message in BC-SYS-DB-MSS if you need help. In all other cases, open a BC-SYS-DB-MSS customer message so the development support team can assist you in finding the cause.
    After you have corrected the root cause, you must remove the shadow tables. You can do this with the latest version of the SAP ABAP program RSDD_MSSQL_CUBEANALYZE (install the latest version which is attached to SAP Note 991014). After the program is installed, execute it with SE38 and choose Settings -> Expert Mode. Acknowledge the warning, press the Detail button under the Determine All Shadow Objects item and press Start Check. Then select each shadow table and press Delete Clone Table.
    For more information regarding SAP BWu2019s use of SQL Server table partitioning, read the white paper titled u201CTable Partitioning in SAP BI on Microsoft?SQL Server# 2005/2008u2033. The paper is currently available for download at:
    http://www.microsoft.com/isv/sap/technology/interop/bi.aspx
    via http://www.bw2048.com/db-shadow-tables-with-k-name-e-g-bicb0000201k0-201.htm

  • Problem with a slash (/) at the end of contextPath

    Hi,
              Weblogic server version 8.1.3.
              We just moved an existing application from tomcat to weblogic. The application context path is "/cm".
              In tomcat the request.getContextPath() returns "/cm"
              in weblogic the request.getContextPath() returns "/cm/" and it causes problems with our external caching product because URL results are double cached "http://server.com/cm/uri" and "http://server.com/cm//uri".
              How can I specify to weblogic web server not to add the "/" at the end ?
              thanks

    This is a bug in 81sp3. Please contact [email protected] in reference to
              bug id CR239600

  • I purchase a ipad from a friend. I set up an apple id on my ipad. Iam trying to set up a itunes account for this ipad, i have had a itunes account in the past, but my apple id is the same only with a 36 at the end of my apple id. When I log into itunes to

    create a new account, it tells me that I already have an account and it won't let me create a new account, how do i cancel that old itunes account and create a new one with my new apple e-mail address?
    Also when I select on my ipad free apps, it pops up with the previous owner apple id on it, how can I change that to my apple id?

    From the information that you have provided, it sounds like your Apple ID has been disabled.  You can read this to see how you can troubleshoot that issue.
    Apple ID is Disabled - Apple Club - Google Sites
    You can try signing out of your ID on the iPad and sign in with his ID and see if that works.
    Settings>iTunes and App Stores>Apple ID. Tap your ID and sign out. Then sign in with your husband's newly created ID.

Maybe you are looking for

  • Passing parameter to group of portlets on a page from 1 report

    Hello Forum, Portal 3.0.7 I have a page (page 2) that contains three portlets, each is a single report (Reports b, c, and d). They all query based upon the same parameter value. I have a different page (page 1) that contains a report (report a). I wa

  • Problem with JQuery in Shell !

    Hallo. When I use JQuery for element HTML working fine JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="http://static.jsbin.- but when I try use this HTML component in Shell JQuery not working JS Bin - Collaborative JavaScri

  • Patch 6.5.2.3

    I have the following problem with the patch 6.5.2.3. In the readme6523.txt file , section INSTALLATION OF PATCH 6.5.2.3 (see also the Notes below), under Note 1, there is an explanation how should one set parser preference in Designer to NO. I'm incl

  • Download Other Backgrounds

    Need to know if you can download other backgrounds for IMovie 09? There just isn't enough!

  • Code doesn't work in browsers

    Hi guys, I am desperate for some help any advice much appreciated! I would be interested to hear if anyone else has had this problem and how they solved it? I am working in MX pro 2004 and Learning Action Script and behaviours and have created a movi