|
I'm very new to C so hopefully this will be an easy question. I'm writing a program in C which is supposed to count the number of lines in a file, then create a 2D int array with a size of size nx3 (n is the number of lines). Here's what I have written to create it: code:
|
# ? Sep 13, 2011 03:45 |
|
|
# ? Jun 8, 2024 20:41 |
|
when you say that it gives you a 4x4 array, how do you know that? and how are you counting the number of lines? TasteMyHouse fucked around with this message at 06:30 on Sep 13, 2011 |
# ? Sep 13, 2011 06:20 |
|
TasteMyHouse posted:when you say that it gives you a 4x4 array, how do you know that? When I noticed things weren't acting like they were supposed to, I started adding printf statements to try to understand what was happening. The prof in my class added a sample input file for me to read in, and that ended up being 51 lines. After I created the array I told it to printf the number of lines and the sizeof the array and array[1], and that returned "51 4 4". Also after I try to put the numbers into an array I had that print out by row and it ended up printing out the first 16 numbers in a 4x4 format. This program is supposed to read vertices in 3D space and blocks of vertices in the file are supposed to form polygons, so between these blocks there's a 'J'. The way I'm counting the lines is as it reads through each individual character, if it reads a number then it marks a bool true, and then once it reaches the new line character if that bool is set to true then it'll increment numOfLines. I manually counted the number of vertices in the file and there were 51, so it looks like that's working properly.
|
# ? Sep 13, 2011 06:44 |
|
C doesn't keep track of array dimensions automatically, you need to do that yourself. I think you'll find that sizeof() is giving you the pointer size (because that's what it knows about at compile-time), which is unrelated to the number of elements in the array.
|
# ? Sep 13, 2011 07:25 |
|
Jabor posted:C doesn't keep track of array dimensions automatically, you need to do that yourself. Oh hey that fixed it. Thanks a lot! Prior to this all I had ever written in was Java so I'm still having trouble wrapping my head around all this.
|
# ? Sep 13, 2011 07:33 |
|
Puppet Pal Claudius posted:After I created the array I told it to printf the number of lines and the sizeof the array and array[1], and that returned "51 4 4". Looks like you already understand why this basic approach doesn't work, but I thought I'd mention that array[1] is not the first element in the array, but the second.
|
# ? Sep 13, 2011 12:08 |
|
TasteMyHouse posted:Looks like you already understand why this basic approach doesn't work, but I thought I'd mention that array[1] is not the first element in the array, but the second. Oh I know that, I just arbitrarily put 1 in there since every row will have the same number of elements in there and also 1 is a nice number.
|
# ? Sep 13, 2011 13:34 |
|
hahaha okay
|
# ? Sep 13, 2011 14:17 |
|
Puppet Pal Claudius posted:Oh hey that fixed it. Thanks a lot! Prior to this all I had ever written in was Java so I'm still having trouble wrapping my head around all this. The C FAQ covers a lot of quirks with stuff like pointers and arrays http://c-faq.com/~scs/cgi-bin/faqcat.cgi It also has a lot of 'old' stuff you probably won't run into, like things about DOS or old versions of UNIX.
|
# ? Sep 13, 2011 14:54 |
|
So, the Windows 8 Developer Preview build is available for the general public: http://msdn.microsoft.com/en-us/windows/home/ It includes the new Visual Studio (express) and probably most interestingly the new Windows SDK which includes WinRT, the C++-based API which is a replacement for (and is not built on) Win32. Downloading it now, but I'm pretty curious about what new C++0x features are in the new compiler. If any.
|
# ? Sep 14, 2011 06:13 |
|
Paniolo posted:It includes the new Visual Studio (express) and probably most interestingly the new Windows SDK which includes WinRT, the C++-based API which is a replacement for (and is not built on) Win32. But I expect it to be a horrible monster more like MFC.
|
# ? Sep 14, 2011 06:46 |
|
Apparently using WinRT requires using C++\CLI style extensions, so MS is kind of full of poo poo when talking about how it's "C++"
|
# ? Sep 14, 2011 07:13 |
|
Unless they mean it isn't C
|
# ? Sep 14, 2011 20:58 |
|
I have a C project that has an obsolete #define that needs to be updated, but in the horrific rat's nest of headers I am having difficulty finding where it is. is there any trick I can pull with gcc to get it to tell me where a #define originated, or am I stuck with using find, grep, and manually tracing through the files?
|
# ? Sep 14, 2011 21:50 |
|
Define it again and the redefinition warning will say where the previous one was.
|
# ? Sep 14, 2011 21:55 |
|
Thanks!
|
# ? Sep 14, 2011 22:37 |
|
So I'm working on a small basic C++ problem. I need to calculate the summation of Lucas series with a user given upper bound number. Basically I'm stuck at the logistic part. I know I will have to use loops (for loops) to find a value c = a + b until c <= upper bound. I just don't know how to set up the loop initiation and condition. Here the excerpt for the problem: code:
|
# ? Sep 14, 2011 23:04 |
|
Another Poster posted:And I know how to check if a number is a Lucas number or not. You shouldn't be checking whether a number is a Lucas number or not. It's not like you're going to iterate through numbers until you get one that's a Lucas number.
|
# ? Sep 14, 2011 23:22 |
|
Another Poster posted:Just looking for hints and pointers here. And I know how to check if a number is a Lucas number or not. Just confuse on for loops summation. I suspect you would want a while loop. You can do it with a for loop, but it would look silly.
|
# ? Sep 15, 2011 01:15 |
|
nop posted:I suspect you would want a while loop. You can do it with a for loop, but it would look silly. Ok yeah, turn out it is a rather simple while loop. Another quick question, how would the condition: "when x=1 or 2 or 3 or 4 or 7 AND y=1 or 2 or 3 or 4 or 7, do stuff" be written?
|
# ? Sep 15, 2011 03:39 |
|
Another Poster posted:Ok yeah, turn out it is a rather simple while loop. code:
It might also be done with a function to search through a const array of ints, which would be more complicated to write but more easily extended, or, in C++, checking against a std::set which would make it extensible to many more checks without taking significantly longer.
|
# ? Sep 15, 2011 03:47 |
|
roomforthetuna posted:
Thank you, just what I was looking for. And so there no way but to type x== each time?
|
# ? Sep 15, 2011 03:52 |
|
Another Poster posted:Thank you, just what I was looking for. And so there no way but to type x== each time? Edit: Or you could do it as a couple of switch statements, in which case you'd have to type 'case' as many times as you'd have to type x== so it wouldn't really help, but it is another way! roomforthetuna fucked around with this message at 03:57 on Sep 15, 2011 |
# ? Sep 15, 2011 03:55 |
|
No, seriously, if you ever need to test whether a number is a Lucas number, you are doing it wrong.
|
# ? Sep 15, 2011 04:14 |
|
roomforthetuna posted:Or you could do it as a couple of switch statements, in which case you'd have to type 'case' as many times as you'd have to type x== so it wouldn't really help, but it is another way! Just wanna point out that switch requires the thing you're comparing against to be known at compile time. you can do this: code:
code:
TasteMyHouse fucked around with this message at 04:43 on Sep 15, 2011 |
# ? Sep 15, 2011 04:41 |
|
If I create a smart pointer (more of a reference) where the semantics for operator= are:code:
Basically, I want the assignment operator to copy the values, and not the pointers, and a seperate rebind method to change the pointer, but I'm not sure how this would work with std::vector After further thought, this is probably a bad idea. I wanted something that would have reference semantics normally, but have pointer semantics inside of a container. tractor fanatic fucked around with this message at 22:57 on Sep 15, 2011 |
# ? Sep 15, 2011 22:10 |
|
Found a list of C++11 features in Visual C++ 2011: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx Pretty disappointing. I was at least hoping for variadic templates and template aliases.
|
# ? Sep 17, 2011 21:10 |
|
The WinRT stuff probably took up all of the developer time that would have been spent on C++11 support. I can't imagine that porting C++/CLI from the CLR to native code was easy.
|
# ? Sep 18, 2011 21:37 |
|
You can't bind a temporary to an lvalue reference, but you can do it with an rvalue reference. VC has a compiler extension that lets you bind temporaries to non-const lvalue references, so does anyone know if the semantics for this are the same as if you had bound the temporary to some non-const rvalue reference, and then took an lvalue reference of the rvalue reference? Like, would this code in MSVC code:
code:
|
# ? Sep 19, 2011 01:48 |
|
Plorkyeran posted:The WinRT stuff probably took up all of the developer time that would have been spent on C++11 support. I can't imagine that porting C++/CLI from the CLR to native code was easy. Yeah, here's a Herb Sutter talk from BUILD where he basically confirms that C++0x features took a back seat to WinRT and GPGPU stuff in this release. But supposedly variadic templates are all but done and will be added in a service pack.
|
# ? Sep 19, 2011 03:35 |
|
I'm trying to create a cleanup function for a matrix class I'm using. I'm attempting to delete a 2D array, but I'm getting an error telling me "pointer being freed was not allocated"code:
|
# ? Sep 20, 2011 05:37 |
|
Jort posted:I'm trying to create a cleanup function for a matrix class I'm using. I'm attempting to delete a 2D array Could you post the code that constructs the array and the code that uses it?
|
# ? Sep 20, 2011 07:53 |
|
I'd like to do some C++ practice, I've mostly stayed away due to not knowing what the gently caress about compilers and mostly do Python. However, i really should learn. Is there a good free compiler I can use to start? I'd like to stick to open/cross platform libraries as much as possible rather than delving into Visual Studio. I've seen devc++, is that good enough?
|
# ? Sep 22, 2011 15:15 |
|
Devc++ as an IDE is pretty poo poo (unless it's changed dramatically within the last year or so, which I doubt), while Visual Studio is probably the best you are likely to get your hands on, and you can write perfectly portable code with it too.
|
# ? Sep 22, 2011 15:41 |
|
Sylink posted:I'd like to do some C++ practice, I've mostly stayed away due to not knowing what the gently caress about compilers and mostly do Python. Dev C++ isn't a compiler -- rather, it is an IDE; it provides a front-end tying together the MinGW version of GCC (the GNU Compiler Collection), GDB (the GNU Debugger), and a simple text editor. It kind of sucks and is poorly supported and (when I last used it in 2009) buggy. If you don't want to use Visual Studio, but (for some reason) still want an IDE, you can check out Eclipse CDT. Eclipse is a free and libre IDE originally developed for Java, but with CDT can compile C and C++. It also uses GCC. If you want to learn about the compilation process, don't use an IDE at all. Just get a text editor (GEdit, Notepad++, Emacs, Vim, whatever) then use MinGW to compile files on the command line. e: if you're really interested in making things cross platform, get yourself some kind of GNU/Linux (virtual box, dual boot, Wubi, whatever) as well. TasteMyHouse fucked around with this message at 19:52 on Sep 22, 2011 |
# ? Sep 22, 2011 15:44 |
|
Sylink posted:Is there a good free compiler I can use to start? I'd like to stick to open/cross platform libraries as much as possible rather than delving into Visual Studio. If free-as-in-price is what you care about, you can download the Win32 SDK for free. This contains the basics like cl (the compiler that Visual Studio uses), the STL libraries, and nmake (the Microsoft make utility). Throw in a C++ aware text editor like TextPad and you are good to go. As a bonus, using cl directly will teach you all the compiler flags.
|
# ? Sep 22, 2011 20:48 |
|
Yah I have a Fedora laptop I use for messing around so Ill check this stuff out. I really want to stay cross platform.
|
# ? Sep 23, 2011 00:36 |
Sylink posted:Yah I have a Fedora laptop I use for messing around so Ill check this stuff out. I really want to stay cross platform. FYI, just using the Visual C++ IDE does not magically make your code become non-standard. There's two ways VC++ can make you write weird code: MFC and C++/CLR (C++ for .NET). With the Win8 Developer Preview there's also a third, C++/CX. But it's not like it writes weird code behind your back, unless you actually tell it to create a project using any of those variations. Just get Visual C++ 2010 Express and start writing standard C++ using a good compiler and wonderful IDE. (Actually, you don't even get MFC with VC Express, so that's one less.) It just seems to me that people have some weird ideas about VC++ making you write terrible non-standard code... maybe that was true 10 years ago with version 6. That's 10 years ago.
|
|
# ? Sep 23, 2011 01:24 |
|
cl.exe is a fairly non-conformant compiler as far as these things go, so there's a lot of things that shouldn't work but do, and a lot of things that should work but don't.
|
# ? Sep 23, 2011 01:33 |
|
|
# ? Jun 8, 2024 20:41 |
|
The other thing VC++ does that's bad for cross-platforming is that it does everything with vcproj files where the more-standard is makefiles. That's pretty unhelpful when it comes to compiling on other systems. (Not that you can't use the compiler with text editors and command lines and stuff, but if you use the IDE then you'll be using vcproj files.)
|
# ? Sep 23, 2011 06:33 |