Passing argument 1 of 'scanf' makes po


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing argument 1 of 'scanf' makes po
# 1  
Old 09-16-2013
Passing argument 1 of 'scanf' makes po

Code:
[CS241C-01@lewis ~]$ cc Array.c
Array.c: In function ‘main’:
Array.c:23: warning: passing argument 1 of ‘scanf’ makes po
Array.c:25: error: expected expression before ‘return’
Array.c:29: error: expected expression before ‘return’
Array.c: At top level:
Array.c:44: error: expected ‘)’ before ‘&’ token
Array.c:46: error: expected identifier or ‘(’ before ‘{’ to
[CS241C-01@lewis ~]$

ok so this is the problem I got. I am trying to input the value to search the value in the array and I don't know what went wrong. Help me.

Code:
login as: CS241C-01
CS241C-01@148.166.200.55's password:
Last login: Mon Sep 16 10:36:56 2013 from ool-addcc5ab.static.optonline.net
[CS241C-01@lewis ~]$ vi Array.c
#include "stdio.h"
#define MAXARRAY 12
#define MAXARRAY1 6
int main()
{
        int a[MAXARRAY];
        int b[MAXARRAY1],
        nvals,
        nvals1;
        int size =0;
        size = sizeof(a)/sizeof(a[0]);
        printf("Array A has %d elements\n ", size);
        int size1=1;
        size1 = sizeof(a)/sizeof(a[0]);
        printf("Array B has %d elements ", size1);
        printf("Input the values in Array A");
        nvals = get_data(a,MAXARRAY);
        printf("Input the values in Array B");
        nvals1 = get_data(b, MAXARRAY1);
        int value;
        printf("Enter the value you wish to find in THREE arrays of floats");
        scanf(value);
        find_value(a,&size,&value);
        if (return == 0)
        printf("The value is found in Array A!");
        else
        find_value(b, &size1, &value);
        if (return == 0)
        printf("The value is found in Array B!");
        else
        printf("The value is not found!");
}
int get_data(int vals[],int max)
{
        int n = 0;
        while (n < max && scanf("%d", &vals[n]) == 1)
        n++;
        return n;
}
int find_value(array, &scale, &num)
int arrary[], scale, num;
{
        for (i = scale -1; i>= 0; i--)
        {       if ( array[i]==num );
                return 0;
        }
                else
                return -1;
}


see if you can help me with this code thanks!

---------- Post updated at 01:50 PM ---------- Previous update was at 01:38 PM ----------

how do I use input in code? do I use "scanf"? and then put the assigned integer variagle next to one like this scanf(number) or scanf"number"? please help!

Last edited by Scrutinizer; 09-16-2013 at 02:58 PM.. Reason: additional code tags
# 2  
Old 09-16-2013
See man scanf for details on scanf.

In general though, scanf should be avoided, since it has problems dealing with rejected input. It can easily work once and fail 5 times afterwards because the first character it reads is \n instead of a digit.

For this reason I suggest the slightly more complicated sscanf, so you can read an entire line then scan it:

Code:
char buf[512];

...

fgets(buf, 512, stdin);
sscanf(buf, "%d", &number);

# 3  
Old 09-16-2013
ok thanks now I have more errors with return that I want them to work.

I need the values to be found or not to be found in arrays.

Code:
Array.c: In function ‘main’:
Array.c:25: error: expected expression before ‘return’
Array.c:29: error: expected expression before ‘return’
Array.c: At top level:
Array.c:44: error: expected ‘)’ before ‘&’ token
Array.c:46: error: expected identifier or ‘(’ before ‘{’ token

Code:
#include "stdio.h" 
02#define MAXARRAY 12 
03#define MAXARRAY1 6 
04  
05int main() 
06{ 
07        int a[MAXARRAY]; 
08        int b[MAXARRAY1], 
09        nvals, 
10        nvals1; 
11        int size =0; 
12        size = sizeof(a)/sizeof(a[0]); 
13        printf("Array A has %d elements\n ", size); 
14        int size1=1; 
15        size1 = sizeof(a)/sizeof(a[0]); 
16        printf("Array B has %d elements ", size1); 
17        printf("Input the values in Array A"); 
18        nvals = get_data(a,MAXARRAY); 
19        printf("Input the values in Array B"); 
20        nvals1 = get_data(b, MAXARRAY1); 
21        int value; 
22        printf("Enter the value you wish to find in THREE arrays of floats"); 
23        scanf("%d", value); 
24        find_value(a,&size,&value); 
25        if (return == 0) 
26        printf("The value is found in Array A!"); 
27        else
28        find_value(b, &size1, &value); 
29        if (return == 0) 
30        printf("The value is found in Array B!"); 
31        else
32        printf("The value is not found!"); 
33} 
34int get_data(int vals[],int max) 
35{ 
36        int n = 0; 
37  
38        while (n < max && scanf("%d", &vals[n]) == 1) 
39        n++; 
40        return n; 
41} 
42  
43int find_value(array, &scale, &num) 
44int arrary[], scale, num; 
45{ 
46  
47        for (i = scale -1; i>= 0; i--) 
48        {       if ( array[i]==num ); 
49                { 
50                return 0; 
51                } 
52        } 
53  
54                return -1; 
55}

---------- Post updated at 02:39 PM ---------- Previous update was at 02:23 PM ----------

what kind of return statement should I use I still have problem with the returned???
# 4  
Old 09-16-2013
You cannot make a variable named 'return' because 'return' is a reserved word.

Half the time you're using 'if(return == something)' you haven't even tried to declare a variable of that name anyway.

Last edited by Corona688; 09-16-2013 at 04:43 PM..
# 5  
Old 09-16-2013
now after fixing the return I got this error: segmentation error


Code:
#include "stdio.h" 
02#define MAXARRAY 12 
03#define MAXARRAY1 6 
04  
05int main() 
06{ 
07        int a[MAXARRAY]; 
08        int b[MAXARRAY1], 
09        nvals, 
10        nvals1; 
11        int size =0; 
12        size = sizeof(a)/sizeof(a[0]); 
13        printf("Array A has %d elements\n ", size); 
14        int size1=1; 
15        size1 = sizeof(a)/sizeof(a[0]); 
16        printf("Array B has %d elements ", size1); 
17        printf("Input the values in Array A"); 
18        nvals = get_data(a,MAXARRAY); 
19        printf("Input the values in Array B"); 
20        nvals1 = get_data(b, MAXARRAY1); 
21        int value; 
22        printf("Enter the value you wish to find in THREE arrays of floats"); 
23        scanf("%d", value); 
24                                       
25        find_value(&a,&size,value); 
26        if (find_value(&a,&size, value) == 0) 
27        printf("The value is found in Array A!"); 
28        else
29        find_value(&b, &size1, value); 
30        if (find_value(&b, &size1, value) == 0) 
31        printf("The value is found in Array B!"); 
32        else
33        printf("The value is not found!"); 
34} 
35  
36int get_data(int vals[],int max) 
37{ 
38        int n = 0; 
39  
40        while (n < max && scanf("%d", &vals[n]) == 1) 
41        n++; 
42        return n; 
43} 
44  
45int find_value(int *array[] ,int *scale,int num) 
46{ 
47        int i; 
48  
49  
50        for (i = *scale -1; i>= 0; i--) 
51        {       if ( *array[i]==num ); 
52                { 
53                return 0; 
54                } 
55        } 
56  
57                return (-1); 
58}

# 6  
Old 09-16-2013
Code:
scanf("%d", value);

should be
Code:
scanf("%d", &value);

Again, I encourage you to read man scanf
# 7  
Old 09-16-2013
ok when I try to enter the value that is suppose to be found in Array B but the output says it was in Array A

Code:
#include "stdio.h" 
02#define MAXARRAY 12 
03#define MAXARRAY1 6 
04#define MAXARRAY2 9 
05  
06int main() 
07{ 
08        int a[MAXARRAY]; 
09        int b[MAXARRAY1]; 
10        int c[MAXARRAY2]; 
11        int nvals; 
12        int nvals1; 
13        int nvals2; 
14        int size =0; 
15        int fetch; 
16        size = sizeof(a)/sizeof(a[0]); 
17        printf("Array A has %d elements\n ", size); 
18        int size1=0; 
19         
20     
21        size1 = sizeof(B)/>/sizeof(b[0]); 
22        printf("Array B has %d elements\n ", size1); 
23        int size2=0; 
24        size2 = sizeof(c)/sizeof(c[0]); 
25        printf("Array C has %d elements\n " , size2); 
26        printf("Input the values in Array A\n "); 
27        nvals = get_data(a,MAXARRAY); 
28        printf("Input the values in Array B \n"); 
29        nvals1 = get_data(b, MAXARRAY1); 
30        printf("Input the values in Array C \n"); 
31        nvals2 = get_data(c, MAXARRAY2); 
32        int value; 
33        printf("Enter the value you wish to find in THREE arrays of floats"); 
34        scanf("%d", &value); 
35        fetch=find_value(&a,&size,value); 
36  
37        if (find_value(&a,&size, value) == 0) 
38        { 
39        printf("The value is found in Array A!"); 
40        } 
41        else
42        { 
43                fetch=find_value(&b, &size1, value); 
44  
45                 if (find_value(&b, &size1, value) == 0 ) 
46                { 
47                printf("The value is found in Array B!"); 
48                } 
49                else
50                { 
51                        fetch= find_value(&c, &size2, value); 
52  
53                         if (find_value(&c, &size2, value) == 0) 
54                        { 
55                         
56                                                                                                        
57  
58                        printf("The value is found in Array C!"); 
59                        } 
60                        else
61                        { 
62                        printf("The value is not found in Three Arrays!"); 
63                        } 
64                } 
65        } 
66} 
67  
68int get_data(int vals[],int max) 
69{ 
70        int n = 0; 
71  
72        while (n < max && scanf("%d", &vals[n]) == 1) 
73        n++; 
74        return n; 
75} 
76  
77int find_value(int *array[] ,int *scale,int num) 
78{ 
79        int i; 
80  
81  
82        for (i = *scale -1; i>= 0; i--) 
83        {       if ( *array[i]==num ); 
84                { 
85                return 0; 
86                } 
87        } 
88  
89                return (-1); 
90  
91}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

3. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

4. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

5. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

6. Programming

warning: passing arg 1 of `inet_addr' makes pointer from integer without a cast

I use solaris10,following is tcp client code: #include "cliserv.h" int main(int argc,char argv){ struct sockaddr_in serv; char request,reply; int sockfd,n; if(argc!=2) err_quit("usage: tcpclient <IP address of server>"); if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0) ... (1 Reply)
Discussion started by: konvalo
1 Replies

7. Shell Programming and Scripting

Passing argument to nawk

Hi all I have got a file digits.data containing the following data 1 3 4 2 4 9 7 3 1 7 3 10 I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (2 Replies)
Discussion started by: ganiel24
2 Replies

8. UNIX for Dummies Questions & Answers

Using * when passing argument to alias

I have some folders with this structure: /data/me/a123xxx Where "xxx" is some changing series of letters, and "123" changes so folders might look like: /data/me/a123xxx /data/me/a234ysd /data/me/a534sds etc. The numbers are what matter to me (they identify the folder), so I created an... (7 Replies)
Discussion started by: ramparts
7 Replies

9. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

10. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question