Problem with array.

This is a discussion on Problem with array. within the C Programming forums, part of the General Programming Boards category; Hi. I'm having problems with an C exercise. So the exercise says: "Make a function with one variable (int k) ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Problem with array.

    Hi. I'm having problems with an C exercise. So the exercise says: "Make a function with one variable (int k) which returns an array of the size of k where every box (sorry for my bad english) of the array is 1." So this is what I did.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int pinakas(int k){
        int i,c[i];
        for(i=0;i<k;i++){
           c[i]=1;
           printf("%d",c[i]);
           }        
    }
    
    main(){
      
      int a;
      printf("Give integer: ");
      scanf("%d",&a);
      pinakas(a);
      system("pause");
    }
    The program works, as long as I put 3 as an integer, if I put a number which is greater than 3 the program crashes. I could do the problem without an array (where instead of c[i] I would put c) and it works fine. So what am I doing wrong here? Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Code:
    int pinakas(int k){
        int i,c[i];
    i is used unintialized here, thus c is of some arbitrary size (did you mean to use k?). Your compiler should warn you about that, so crank up the warnings or get a new one.

    Code:
    main(){
    It's int main(void) and return an integer at the end (usually 0). Read why it matters here.

    EDIT: You also don't return anything from pinakas. Either define it with void return type or return a sensible integer.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I didn't see the c[i] mistake I did. Thanks for pointing out the main() thing, but I can't remember my teacher telling us that we have to write int main(void) and it should return 0. The way I first wrote it is the way we've learned it...
    Last edited by Fink; 04-11-2011 at 03:00 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Well, your teacher is teaching you crap. Read the link I posted regarding the problems with what you're doing (click the red text in my previous post). Perhaps even explain to your teacher you heard that his/her way is wrong and you want to know whether it's really wrong and why you're being taught that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  4. array problem?
    By ssjnamek in forum C Programming
    Replies: 14
    Last Post: 02-08-2006, 05:03 PM
  5. array problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21