-7
void main()
 {
 int a=10;
 int *j;
 *j=&a;
 b[]={1,2,3,4};
 b=j;
 }

Some one asked me is there any problem in this program ,i am just confused for me everything seems fine. Just curious to know.

Shreesh
  • 1
  • 1
  • 2
  • 1
    Welcome to Stack Overflow. Please read the [About] page soon, and also [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336). With a question like this, the immediate reaction is [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) with the obvious suggestion that you try compiling the code. There's plenty there for the compiler to complain about. – Jonathan Leffler Jan 13 '15 at 07:19
  • You've not read the link referenced. The code is only safe from castigation for the return type of `main()` if you're programming on Windows with a Microsoft compiler. – Jonathan Leffler Jan 13 '15 at 07:24
  • Sorry, thanks for your valuable comment ,i was just reading the link.Next time i will keep it in mind – Shreesh Jan 13 '15 at 07:48

1 Answers1

5

Turn on all your compiler warnings and errors. Then it will tell you exactly what is wrong with the program.

*j = &a; is a constraint violation. *j has type int but &a has type int * which is incompatible.

You might have meant j = &a; which will point j to a.

b[]={1,2,3,4}; is a syntax error. Maybe you meant int b[]={1,2,3,4}; which would declare an array.

b=j; is a constraint violation because b is an array and arrays cannot be assigned to. (Technically: because b is an array, decays to an rvalue and rvalues cannot be assigned to).

However, j = b; would be OK and it would make j point to the first member of b;

void main() is non-portable, it should be int main() .

M.M
  • 138,810
  • 21
  • 208
  • 365
  • A big thanks to your clear explanation. I want to improve my skills on c, will you recommend me any book or website where i can learn and expertise c. – Shreesh Jan 13 '15 at 07:24
  • 1
    @Shreesh see [this thread](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for book recommendations – M.M Jan 13 '15 at 07:25
  • Practice, practice, and more practice. There are many decent books on C; there are also many poor ones. The [ACCU](http://accu.org/) has many book reviews for C books. King's [C Programming — A Modern Approach](http://www.amazon.com/Programming-Modern-Approach-2nd-Edition/dp/0393979504) is at least decent ([ACCU](http://accu.org/index.php?module=bookreviews&func=search&rid=1260)). – Jonathan Leffler Jan 13 '15 at 07:29