CornBurrito
Member
Is there a good guide for C syntax? I know some C++ already, but I imagine there are significant differences.
I hear the C Programming Language book is the best you can do. If you need C for a particular goal you could probably choose something more tailored.Is there a good guide for C syntax? I know some C++ already, but I imagine there are significant differences.
I hear the C Programming Language book is the best you can do. If you need C for a particular goal you could probably choose something more tailored.
Is that available as a free online resource?
I know this isn't what you asked, but... why do you want to learn C?
Don't think so, but if you have access to a CS library then it would definitely have a copyIs that available as a free online resource?
A course I am taking requires us to write the programs in C, despite a previous course (and most of the rest of the courses) using C++. No fucking clue why, especially since it is about data structures and algorithms.
But basically I need to write my programs in C now and I just need a basic syntax guide so I'm not scratching my head trying to figure out "ok why isn't std::cout << "Hello World" << std::endl; working?"
Pretty much all you need to know is:
1) All header files end in .h. If you see a header file that has no file extension, like <vector> or <iostream>, it's C++. Don't use it.
2) Anything that starts with std:: is c++, don't use it. google for alternatives. For example, C equivalent of cout
3) Don't use classes or templates, but structs are ok.
4) structs can't have member functions, only data.
That's pretty much 99% of what you need to know, if you start from those assumptions, you can figure out the rest with documentation and google searches.
Wait they're bringing an entire Ubuntu system over? I'd rather just use Ubuntu...
And lol at emacs
"Right, so just Ubuntu running in a virtual machine?" Nope! This isn't a virtual machine at all. There's no Linux kernel booting in a VM under a hypervisor. It's just the Ubuntu user space.
"Ah, okay, so this is Ubuntu in a container then?" Nope! This isn't a container either. It's native Ubuntu binaries running directly in Windows.
"Hum, well it's like cygwin perhaps?" Nope! Cygwin includes open source utilities are recompiled from source to run natively in Windows. Here, we're talking about bit-for-bit, checksum-for-checksum Ubuntu ELF binaries running directly in Windows.
[long pause]
"So maybe something like a Linux emulator?" Now you're getting warmer! A team of sharp developers at Microsoft has been hard at work adapting some Microsoft research technology to basically perform real time translation of Linux syscalls into Windows OS syscalls. Linux geeks can think of it sort of the inverse of "wine" -- Ubuntu binaries running natively in Windows. Microsoft calls it their "Windows Subsystem for Linux". (No, it's not open source at this time.)
Yes, but I'm wondering...Yeah, this is VERY cool, and I just got around using cygwin too. (I do not see the issue with this one YET, but Ill gladly use the native solution over it.)
struct employee{
int id;
int company_rank;
};
struct employee* allocate(){
//Allocate enough memory to hold up to 10 employees
//return the pointer
}
Yes, but I'm wondering...
Using Cygwin for creating Windows binaries is far from perfect, but at least, you can distribute it with only the cygwin dll and possibly a couple others dll.
Will Windows run in *standard* Linux binaries, or will you need to install something (big)?
Also, there's already few users of powershell, that won't help :/
That was my impression, but I may be wrong.Few users of Powershell?
Few users of Powershell?
It's a huge part of the Windows ecosystem. This won't change that.
Short answer: yes, probablyThe struct employee has two integers in it. An integer is 4 bytes. So does this mean each struct is 8 bytes total, and my pointer needs to have 80 bytes allocated to it?
Short answer: yes, probably
Longer answer: is it a question about the space it'll take in memory, or do you intend to try to allocate the memory yourself?
It's tricky, and you probably want to be careful. int size can change, and alignment issues can happen. although in this case, I expect 4-bytes objects be packed back-to-back (and any address got with allocation aligned on adress multiple of 4).
There's a reason even C had "sizeof"...
You'll probably be interested in this:
http://www.catb.org/esr/structure-packing/
Only in the IT / admin community. Developers don't use it at all.
That was my impression, but I may be wrong.
It's just I've never seen a single one in flesh, actually... Barely even met someone who KNOWS it exists IRL. Granted, academics in CS are often closer to Linux than Windows, so it doesn't help, but still...
Only in the IT / admin community. Developers don't use it at all.
Yeah, or you can use calloc which separates size and number of elements to allocate for (it also zeroes the allocated memory).Discovered sizeof after I made the post lol.
I imagine:
struct employee *ptr = malloc(sizeof(struct employee)*10);
works?
Not your fault. More modern languages prevent stuff like that.I kept wondering where I was going wrong on this Bubble Sort algorithm since it never ended and it turns out my conditional check was using = instead of ==....
I thought I was past these sorts of errors.
Wait they're bringing an entire Ubuntu system over? I'd rather just use Ubuntu...
And lol at emacs
Good for the enterprise.
You MUST call it once.Ok so I have
struct employee *ptr from before. I gave it enough memory to hold 10 employee structs.
If I want to free the memory I allocated to it, do I just call free once? Or do I need to free ptr[0], ptr[1], etc...?
I think I only need to call free once?
You'll never be.I thought I was past these sorts of errors.
I'd add a (struct employee*) before malloc to make the cast explicit, but yes.Discovered sizeof after I made the post lol.
I imagine:
struct employee *ptr = malloc(sizeof(struct employee)*10);
works?
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:
Code:cout << "\nPlease enter the name of one of the above teams: \n"; getline(cin, team); ifstream inputFile2; inputFile2.open("WorldSeriesWinners.txt"); while (!inputFile2.eof()) { getline(inputFile2, compare); if (team == compare) { count =+ 1; //count++; } } inputFile2.close(); cout << "The number of times " << team << " has won the World Series between 1903 and 2012 is: " << count << endl;
------------------------------------------------
The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.
I appreciate any help and/or suggestions.
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:
cout << "\nPlease enter the name of one of the above teams: \n";
getline(cin, team);
ifstream inputFile2;
inputFile2.open("WorldSeriesWinners.txt");
while (!inputFile2.eof())
{
getline(inputFile2, compare);
if (team == compare)
{
count =+ 1;
//count++;
}
}
inputFile2.close();
cout << "The number of times " << team << " has won the World Series between 1903 and 2012 is: " << count << endl;
------------------------------------------------
The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.
I appreciate any help and/or suggestions.
It was originally count +=1, but I made a mistake typing it in again it after using count++.count =+ 1;
Try count += 1 or count++ instead
Or is the commented out count++ something you already tried that for some reason did not work?
Does your program ever enter that if conditional successfully to increment that counter? What does your file input look like? If the comparison is false, first look into your string variables to see what's in them (before the if statement, print to console both strings) and then maybe look into trimming your input of leading and trailing white space characters. For example,
"Cubs" and "Cubs " will not match with your code as it is (Cubs 2016 WS champs believe it!)
How are declared team and compare?I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:
Code:if (team == compare)
The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.
I appreciate any help and/or suggestions.
Yes, sorry... I've just tried and it even don't accept a char buffer, anyway.Both team and compare are declared as strings, so I don't think the program is comparing the pointers but rather the content of the variables themselves i.e the strings.
Yes, sorry... I've just tried and it even don't accept a char buffer, anyway.
I must be oversensitive to this today (too much code from student to read, and with == / != / is / is not in Python translating to = / <> / == / != in ML, it's a nightmare catching all those tricky bugs.
Beside, I've been burned before with string comparisons...
Maybe you could write the content of each string as a list of hex values, and check whether they have something different in them?
Unfortunately, I'm not fresh today ^_^I just wanted a fresh pair of eyes
Unfortunately, I'm not fresh today ^_^
I may suspect something with \r at the end of the line... If lines in the file are \r\n terminated, getline will return the line without \n but with \r IIRC.
Since it's not a printable character and the display of \r in a terminal can be silent, that could be a possible reason...
Probably none, it's a matter of removing an unwanted, invisible character...So if the line is:
New York Yankees
with nothing else shown in the text file, what suffix am I supposed to add?
I've sent you an MP, but since your code works for me when I set properly the end of line characters in the text file, I'm more and more convinced that's the issue.Edit: I had the program read from the same text file in the comparison (the original one with the team list) and it didn't give me a count of one like it was supposed to, meaning the issue most probably isn't the textfile.
cout << (int)(team.at(team.length()-1)) << " " << (int)(compare.at(compare.length()-1)) << endl;
You MUST call it once.
You have to free memory from malloc with exact mirror calls to free (the order does't matter)
For each pointer given by malloc, you need to call free with the same pointer, and only once.
You cannot write free(ptr[1])
You'll never be.
I'd add a (struct employee*) before malloc to make the cast explicit, but yes.
For each pointer given by malloc, you need to call free with the same pointer, and only once.
Good one... Not sure what you're after.Asshole pedantic question: What is the exception to this rule?
Part of your original comment was that you must never call free more than once on the return value of malloc. There's an exception to that, a time when you theoretically canGood one... Not sure what you're after.
Since freeing NULL is guaranted to be safe, I guess it's not when malloc fail.
I suppose that cases where the memory can be freed elsewhere, including when there's a fork between the malloc and the free don't count...
Any hint?