LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2019, 12:02 PM   #406
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 149

Rep: Reputation: 111Reputation: 111

For example:

varPtr = (double *) malloc(1000 * sizeof(int))

Will not change those 1000 ints into 1000 doubles. It will only typecast the value returned by malloc.
 
1 members found this post helpful.
Old 06-13-2019, 02:26 PM   #407
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,831

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Quote:
Originally Posted by jsbjsb001 View Post
Thanks guys.

It is a little more clear now, although I can't say I completely understand yet. I'll probably have to do some more reading of both your posts, and the book to "fully get it".

By "all integers", I meant does the * mean "all" ? Like if I typed rm *.txt I know this means "delete all files with the extension .txt", so does the * have the same meaning in C ? (I know the * also means multiply, and it defines pointers in C as well)

I did read about the free() function, but I totally forgot to include it in my code. But yes, I see your point Mechanikx. So I'll try to remember to do that next time, and thanks for the example too.

Just to be clear: The * also means void ?
Not void. The type is "int *", which is a "pointer to an int". A void pointer is "void *", meaning a pointer to nothing.

The current C standard allows a pointer to void to be elevated to a pointer to any type, which is why the assignement "intPtr = (int *) malloc..." could be "intPtr = malloc..." because malloc is defined to return the the type "void *".

Some people prefer to keep the cast as it makes the code compatible with older standards, and is not rejected by the current standard. Other people just think it makes the code clearer as declaring that it will be a specific type (the cast). The compiler should then complain if the target variable (the intPtr in this case) is not the same type as the cast. This is considered good, as it would be a programming error if it didn't, and thus expose a potential bug if it doesn't match. And that bug can happen if future updates did change the type of the variable "intPtr".
 
2 members found this post helpful.
Old 06-13-2019, 08:00 PM   #408
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 149

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by jpollard View Post
The only systems I know of that don't necessarily clean up on exit are in embedded software - and there may not be an operating system as expected.
I was trying to think of a non-embedded system that doesn't clean up and I thought of TempleOS. I haven't looked into it, and I realize it's more of a niche system, but from what I've heard it doesn't strike me as a system that would clean up for you.
 
Old 06-14-2019, 05:08 AM   #409
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,831

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Quote:
Originally Posted by Mechanikx View Post
For example:

varPtr = (double *) malloc(1000 * sizeof(int))

Will not change those 1000 ints into 1000 doubles. It will only typecast the value returned by malloc.
yes. But that will make only half the expected number of doubles. 500 instead of 1000.
 
1 members found this post helpful.
Old 06-14-2019, 07:10 AM   #410
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,794

Original Poster
Rep: Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439
Thanks Mechanikx and jpollard!

I think I get what you guys are saying now. But just to make sure; the * means it's a pointer to something, be that int, void, whatever comes before the * ? Hopefully I get it now - or at least that part of it anyway.

I've finally got to the "debugging" chapter anyway's, and even better; it focuses on gdb! It was meant to be... yeah I know, but I couldn't help myself

But all jokes aside; I re-compiled it with the -g option like the C book said, and I think I might have figured out at least part of the reason why that function I added to the program astrogeek wrote me goes into an infinite loop when I don't enter in a number, and enter in letters instead. From what gdb is showing me, the same number previously entered is still the value of the "x" variable - although, while I'm sure that's important... I'm not exactly sure why it causes the infinite loop. But I think it is the reason (or a part of it).

But I still have no idea why it isn't displaying the "binary output" in the right place tho (in the base 10 Decimal column, instead of the base 2 binary column).

I've posted my debugging attempt below with gdb's output debugging the program;

Code:
[james@jamespc math-helpers]$ gdb bases_binaryFunction
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/james/temp/devel/math-helpers/bases_binaryFunction...done.
(gdb) break binaryOutput
Breakpoint 1 at 0x4006b8: file bases_binaryFunction.c, line 46.
(gdb) run
Starting program: /home/james/temp/devel/math-helpers/bases_binaryFunction 

Enter a positive integer to convert (0 to exit): 5

Base 10     Base 8     Base 16    Base 2     
(Decimal)   (Octal)    (Hex)      (Binary)   
=========   =======    =======    ===========

Breakpoint 1, binaryOutput (x=5) at bases_binaryFunction.c:46
46                      for(pow2=1;;pow2 *= 2)
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.5.x86_64
(gdb) print x
$1 = 5
(gdb) continue
Continuing.
01015           5           5           0
Enter a positive integer to convert (0 to exit): asd

Base 10     Base 8     Base 16    Base 2     
(Decimal)   (Octal)    (Hex)      (Binary)   
=========   =======    =======    ===========

Breakpoint 1, binaryOutput (x=5) at bases_binaryFunction.c:46
46                      for(pow2=1;;pow2 *= 2)
(gdb) print x
$2 = 5
In any case; thank god for you guys (and the others that have responded too) - as I donno what I'd do if weren't for you guys... I really don't. I'm very grateful to ya's, more than you's know. Thanks again for all the help and insights.
 
Old 06-14-2019, 08:26 AM   #411
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,831

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Quote:
But all jokes aside; I re-compiled it with the -g option like the C book said, and I think I might have figured out at least part of the reason why that function I added to the program astrogeek wrote me goes into an infinite loop when I don't enter in a number, and enter in letters instead. From what gdb is showing me, the same number previously entered is still the value of the "x" variable - although, while I'm sure that's important... I'm not exactly sure why it causes the infinite loop. But I think it is the reason (or a part of it).
I believe that is because scanf doesn't advance the file pointer when it detects a non-digit. And since it didn't get a number, it never replaces the old value.

So the next time the program reaches the scanf, the same thing happens... hence the "infinite loop".

One way to fix it is to assigne the return value of the scanf (it returns the number of fields with a successful conversion). In your case, the scanf should always return 1 (there is only one format field specified), and if it returns 0 then there was some kind of error.
The "if" following checks to see if the number read was 0... so just replace the variable with the one you assign the return value to and the "infinite loop" should disappear.
 
1 members found this post helpful.
Old 06-16-2019, 05:09 PM   #412
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Debian64 stable, LFS 7.1
Posts: 480

Rep: Reputation: 138Reputation: 138
jsbsb, I remember you started working with arrays but how much experience do you have with using pointers so far?
 
Old 06-16-2019, 06:30 PM   #413
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Quote:
Originally Posted by jsbjsb001 View Post
Hi Guys/Gals.

So, as the title says, I'm currently trying to learn C. I guess first it probably helps if I give some background;

Background:

While I'm not new to computers, Linux, operating systems in general, etc, I can't really say the same for C. I did understand a tiny bit, but no where near enough to actually be able to write a program myself (and still am pretty green with it). Haven't had any real experience with it - or at least what most people I know would consider to be proper experience.

I'm doing this for a few reasons; the main reason is that, I'm at that stage of life where I want to try and find something that I can not only do, but ENJOY too. Sounds easy right? Well no, it isn't. So I figured if I commit myself to at least getting though the tutorial I'm currently following, and hopefully this can give me a clear cut answer to that question. And yes, I'm sick of looking at source code and not being able to understand the code properly.

Just in case I have anymore dumbass questions for ya's, I'll leave this thread open until I'm sure that I have no questions - rather that opening thread after thread.

My Questions:

1) I know there is a C function for opening files (the chapter I'm currently up to), but there is also a kernel system call that also opens files, being the open() function. I don't understand the overlap and I can't find anything that really makes a lot of sense to me at this point. Is there any reason for this? Which should I use? I don't get it, I'm sorry.

2) I've also attached some code that I've been playing with, but I'm just too green to really understand how good or bad it is - it does compile and work (I even fixed the loop in it too). So I guess I'm asking if people that know more (hopefully a LOT more) than myself could just have a look at it, and see if there is something that I missed/written incorrectly, etc. I know that sounds stupid, but it really would be good for me to get more than just my eyes reviewing it.

3) Is there anything you can recommend to make life simpler?

4) While this maybe a question for later on; is there any real benefit to using an IDE? As right now I'm just using Kwrite and gcc directly.

Please don't hold back if you can see something that could be better. But please remember that I am the newbie here in this case, so please treat me like I'm 5 years old and need it in simple terms. I've also tried to add my own comments as well - I basically took the example code, and added my own bits and pieces to it.

Don't worry, I'm not expecting to be spoon-fed here and I'm not asking for that. As that does nothing to help me learn - I want to be to do it one day and solve my own problems (as I pretty much already do with any Linux problems I may have). So I'll still be practising anyway.

It's this tutorial that I'm currently following, and I'm up to "C File I/O".

Thanks in advance for any help.

James
I have been working on C for a few months, I have a really hard time myself; but it does get more and more fun the more you understand it. I am lucky enough to have found classes for it (as I couldn't get very far in K&R on my own), perhaps there may be a group or class near by you that you could join? Working with others may help you enjoy it more.
 
1 members found this post helpful.
Old 06-16-2019, 08:31 PM   #414
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 116

Rep: Reputation: 16
Quote:
Originally Posted by FOSSilized_Daemon View Post
I have been working on C for a few months, I have a really hard time myself; but it does get more and more fun the more you understand it.
What have you learned so far? Have you made anything interesting?
 
Old 06-16-2019, 08:39 PM   #415
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Quote:
Originally Posted by Contrapak View Post
What have you learned so far? Have you made anything interesting?
I have learned a lot, about both C and how computers work in general. I try my best to work at each problem and exercise until I really understand it and am able to do it in as few lines as possible. All of my course work in avaible at my repository here: https://gitlab.com/Puffles_the_Dragon/coursework (I just have to add a disclaimer, there are common exercises such as fizzbuzz and parts of K&R here. If you find this post in the feature I encourage you to try your best to work through the exercises to the best of your ability before reivewing the answers. I have no issues with you sharing or coping my work, but don't blame me if you turn in my work and get caught for cheating as I am not responsible for your actions) Sorry, I only add that as I have had something to that affect happen once before and don't want any getting angry at me for making my work public. The biggest things I am working on these days is understanding basic networking in C as I am writing my own package manager for a system me and a buddy are building which is located in the Core repository on the same account. I am after that going to be writing my own version of certain utilities I use, I already wrote my own echo. I also have many scripts and documentation on other repos under that account that I wrote (most notably for Alpine Linux), but those are unrelated to C.
 
1 members found this post helpful.
Old 06-17-2019, 12:57 AM   #416
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,794

Original Poster
Rep: Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439
Thanks again jpollard. That makes sense, so at least I know why the infinite loop is happening. But still have no idea why printf won't display the result properly tho.

Quote:
Originally Posted by Andy Alt View Post
jsbsb, I remember you started working with arrays but how much experience do you have with using pointers so far?
Some, but I will need some more tho. I'm nearing the end of the book, and have read most of the chapters now. But because of some both related, and unrelated hold-ups, it's slowed me down a bit. But that said, I think I will need to re-read the chapter about pointers, and at least a few of the other chapters as well.

I think I get the basic idea, but there's still a few things I'm not particularly clear on. I think at the end of the day, it will come down to actually writing some more code to imprint it in my head, before I can say "I finally get it 100%".

Quote:
Originally Posted by FOSSilized_Daemon View Post
I have been working on C for a few months, I have a really hard time myself; but it does get more and more fun the more you understand it. I am lucky enough to have found classes for it (as I couldn't get very far in K&R on my own), perhaps there may be a group or class near by you that you could join? Working with others may help you enjoy it more.
Yes, as I've also found out, so I agree with you there. I think as far as classes are concerned, and as I was saying above; it's going to take some more reading, and practice before it totally makes sense. But I CAN say it makes a lot more sense generally speaking now than what it did before. I also think I can learn it without taking any programming classes, with the C book I'm reading and the pointers from the guys that have responded to this thread.

Quote:
Originally Posted by Contrapak View Post
What have you learned so far? Have you made anything interesting?
This thread is long enough as it is; please either start a new thread, or use a different thread for this. Thanks.
 
Old 06-17-2019, 10:20 AM   #417
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 116

Rep: Reputation: 16
Quote:
Originally Posted by jsbjsb001 View Post
This thread is long enough as it is; please either start a new thread, or use a different thread for this. Thanks.
Why does something like my question need to be its own thread? Isn't it relevant to OP's journey to learn C?
 
1 members found this post helpful.
Old 06-17-2019, 10:55 AM   #418
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,794

Original Poster
Rep: Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439
Quote:
Originally Posted by Contrapak View Post
Why does something like my question need to be its own thread? Isn't it relevant to OP's journey to learn C?
Because for one, that OP would be myself. Two, this thread is about learning C, not what someone else has learnt/written. Thirdly, you are therefore hijacking the thread. Again, either start your OWN thread or use a different thread.
 
Old 06-17-2019, 11:12 AM   #419
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,779
Blog Entries: 13

Rep: Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272
Quote:
Originally Posted by Contrapak View Post
Why does something like my question need to be its own thread? Isn't it relevant to OP's journey to learn C?
Quote:
Originally Posted by jsbjsb001 View Post
Because for one, that OP would be myself. Two, this thread is about learning C, not what someone else has learnt/written. Thirdly, you are therefore hijacking the thread. Again, either start your OWN thread or use a different thread.
In all fairness, if FOSSilized_Daemon's inputs about learning the C language are welcome, as they should be, then follow-up questions and discussion about those inputs should also be welcome.
 
1 members found this post helpful.
Old 06-19-2019, 01:02 AM   #420
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,645

Rep: Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482Reputation: 2482
I haven't read all 28 pages of this thread ( ), but as far as books go, K&R is a classic, but a bit terse in my opinion.

To learn C (especially for a relatively newbie to programming) I'd recommend https://www.amazon.com/Book-Programm.../dp/0201183994.
It teaches each thing with example code that is then explained...

For more advanced people I can highly recommend https://www.amazon.com/dp/0131774298/
 
1 members found this post helpful.
  


Reply

Tags
c programming, learning c


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration