LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-24-2004, 04:03 PM   #1
Hockeyfan
Member
 
Registered: May 2004
Distribution: SuSe 10.0
Posts: 154

Rep: Reputation: 30
having problems reading data into a C program


I am rewriting all my programs that I wrote in Java while I was in school in C and C++ so that I am comfortable writing in these languages.

My problem is that I am getting a segmentation fault in my program after I do a scanf. I am not sure what I am doing wrong. It has been about 3 years since I have programmed in C.

Here is the code:

Code:
#include <stdio.h>

int main (void)
{
     int num1, num2, num3, num4;
     double dub1, dub2, dub3, dub4;
     char char1, char2, char3;

      scanf("%d, %d, %d, %d", num1, num2, num3, num4);
      printf( "%d\n", num1);
      
      return 0;
}
I have tried debugging the program and changing the variables to pointers. None of these solutions worked. I know it must be a simple thing bukt I am missing it. Any help would be appreciated.

Hockeyfan
 
Old 05-24-2004, 04:07 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you need to pass the address of each of those variables.

scanf("%d, %d, %d, %d", &num1, &num2, &num3, &num4);

u said u tried pointers, i assume u mean this:

int *num1, *num2, *num3, *num4;

those pointers dont actually point into valid memory, same as in java when u do this:

my_object foo;

u have to allocate foo before u use it, same as in C/C++.
 
Old 05-24-2004, 04:51 PM   #3
Hockeyfan
Member
 
Registered: May 2004
Distribution: SuSe 10.0
Posts: 154

Original Poster
Rep: Reputation: 30
Thanks for the help, I knew it was something simple.
 
  


Reply


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
Problems with reading back serial binary data esi-eric Linux - Software 15 10-03-2018 09:13 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM
problems reading audio CDs, data DVDs and CD-Rs umberleigh Linux - Hardware 4 07-29-2004 07:46 AM
reading data from a file afrm Programming 2 12-16-2003 07:43 PM
Reading COM Port Data croakofonix Programming 1 06-11-2003 04:17 PM

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

All times are GMT -5. The time now is 05:20 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
Open Source Consulting | Domain Registration