-3
#include<stdio.h>
#include<conio.h>
#include<string.h>


int fun1(int *_array)
{
    for(int i = 0; i < 5; i++) {
        printf("\nenter an input\n");
        scanf("%f", &_array[i]);
    }

}
int main()
{
    int _array[5];
    int sum;

    fun1(&_array[5]);

    printf("\nInput    Value    Address\n");
    for(int i = 0; i < 5; i++) {
        printf("%d          %_array          %_array\n", i, _array[i], &_array);
    }
    for(int s = 0; s < 5; s++) {
        sum += _array[s];
    }
    printf("The sum of these values is %d", sum);
    getch();
}

I try to compile this and I get a long list(28 errors) of errors. They appear to be mostly syntax and undeclared identifier errors, in the three for loops I have, but they don't make sense because the loops and counters appear to be written and declared properly. Can any one explain/see what I am doing wrong?

I am also encountering similar issues on other programs I have written recently that include for loops, so any help/insight would really help

alk
  • 69,737
  • 10
  • 105
  • 255
  • First, you should return a value for `int fun1()`. – herohuyongtao Mar 17 '14 at 16:59
  • 3
    This: `%_array` is not a valid format specifier in printf, use `%p` for the pointer address and `%f` for the float. – jpw Mar 17 '14 at 17:00
  • 2
    I don't think that I've ever seen `%_array` as a valid conversion character for printf... – ciphermagi Mar 17 '14 at 17:00
  • 3
    And you need to post the errors here. – herohuyongtao Mar 17 '14 at 17:00
  • 4
    I suggest you to pick a basic C book, read it and then...... – haccks Mar 17 '14 at 17:02
  • 1
    I'm not sure why those spaces are there in the printf statement...try `%d\t%d\t%f\n` – ciphermagi Mar 17 '14 at 17:03
  • [this book](http://shop.oreilly.com/product/9780596006976.do) is undoubtedly awesome. I have the C++ version and have seen no equivalent. – motoku Mar 17 '14 at 17:06
  • Try "A Book on C" and "The Practice of Programming" to start off. – ciphermagi Mar 17 '14 at 17:06
  • @SeanPedersen: 600++ pages? This ought to be a big nut ... ;-) – alk Mar 17 '14 at 17:08
  • 3
    This question appears to be off-topic because the code far too many errors to be addressed in a single post. Please refer to a tutorial instead. – devnull Mar 17 '14 at 17:09
  • @alk it's coming in my mail today :) – motoku Mar 17 '14 at 17:09
  • _any help/insight would really help_ -- the only insight I can offer is: **never** post a question generated due to errors without also posting at least _some_ of those errors! – mah Mar 17 '14 at 17:16
  • I'm using basic C, and I've been programming in C# and C for atleast a year now. I know how to write a for loop. I said I've been having this issue as of *recent* as in, last week, I was working on labs for arrays, pointers and strings, and upon writing this, and a couple of other labs of this sort, I hit the following errors: – user3429839 Mar 17 '14 at 17:21
  • I think you forgot to add the errors again. – ciphermagi Mar 17 '14 at 17:23
  • Error1 error C2143: syntax error : missing ';' before 'type' Error2 error C2143: syntax error : missing ';' before 'type' Error3 error C2143: syntax error : missing ')' before 'type' Error4 error C2143: syntax error : missing ';' before 'type' Error5 error C2065: 'j' : undeclared identifier – user3429839 Mar 17 '14 at 17:24
  • omg...seriously? Look for the lines where the semicolons are missing! Then look at the line above them and add one! – ciphermagi Mar 17 '14 at 17:26
  • those are just 5 of them. there are 28 in total, and these 5 are repeated atleast 3 times, making up 15 of those errors. some of them even appear to be repeats. they direct me to the declaration of the for loops every time, but dont specify an exact point, just the line. – user3429839 Mar 17 '14 at 17:27
  • You're telling me I'm missing a semi colon for(int i = 0; i < 5; i++) <-- here? Or you're saying I'm missing one before it. And in the case of the function, there is nothing before that. – user3429839 Mar 17 '14 at 17:28
  • At this point I can almost guarantee you're using a C89 compiler, and that your for loop is complaining at you because you're trying to declare the variable inside the loop. *Try* to resolve the errors using a debugger, by checking man pages, and by just trying to *read your own code.* Then come back and ask us again. – ciphermagi Mar 17 '14 at 17:32
  • The reason I came here is to have a different set of eyes look at my code. I'll try debugging it manually and trying the suggested changes, but the reason I don't suddenly notice all of the errors is because I've been working at this for a while, and visual studio is not pointing out the exact errors I'm looking for. I may not be a professional programmer, but I'm not a dunce either – user3429839 Mar 17 '14 at 17:53

1 Answers1

2

Perhaps you are using a C89 compiler?

C99 introduced a few new things, among which are the definition of variables in the for control group.

for (int i = 0; i < 1; i++) printf("%d\n", i); // only C99
//   ^^^^ new stuff in C99

The same loop, in C89 syntax would have to be

int i;
for (i = 0; i < 1; i++) printf("%d\n", i);
pmg
  • 106,608
  • 13
  • 126
  • 198