Problem with pointers

This is a discussion on Problem with pointers within the C Programming forums, part of the General Programming Boards category; I must write a program that reads in a file of ints into an array and lets the user choose ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    13

    Problem with pointers

    I must write a program that reads in a file of ints into an array and lets the user choose the array index he wants to see. I can read in the file but when assigning it to an array value I obviously screwed up somewhere because it doesn't work, so here is my code for the main function and the function I am having problems in.

    Code:
    #include "my.h"
    
    int main(int argc, char* argv[]){
    FILE *fpIn;
    int *arr;
    int lim=0;
    int j=0;
    
    
      if(openFiles(&fpIn,argv)){
         lim=getInput (&fpIn, &arr);
         oddEvenSort(arr,lim);
     do{
    printf("\nPlease enter a number between 1 and %d or -1 to exit\n",lim);
    scanf("%d",&j);
    if(j>0&&j<=lim)
    printf("\n%d", arr[j]);
    else if(j==-1)
    printf("\ngoodbye\n");
    else
    printf("\nWhoops, that number won't work\n");
    }while(j!=-1); }
      else
    printf("\nOh Snap, I couldn't open the file.\n");
    
      fclose(fpIn);
    
    }
    Code:
    #include "my.h"
    
    int getInput (FILE** fpIn, int** a)
    {
    int i,j;
      fscanf(*fpIn, "%d",&i);
    *a= (int*) calloc(i,sizeof(int));
    for(j=0;j<i;j++){
    fscanf(*fpIn, "%d",&*a[j]);
    }
    return i;
    
    }
    any help will be greatly appreciated.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Perhaps you should start by fixing your indentation...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Re-indented (one-time)
    Code:
    #include "my.h"
    
    int main(int argc, char* argv[]){
      FILE *fpIn;
      int *arr;
      int lim=0;
      int j=0;
    
      if(openFiles(&fpIn,argv)){
        lim=getInput (&fpIn, &arr);
        oddEvenSort(arr,lim);
        do{
          printf("\nPlease enter a number between 1 and %d or -1 to exit\n",lim);
          scanf("%d",&j);
          if(j>0&&j<=lim)
            printf("\n%d", arr[j]);
          else if(j==-1)
            printf("\ngoodbye\n");
          else
            printf("\nWhoops, that number won't work\n");
        }while(j!=-1); 
      }
      else
        printf("\nOh Snap, I couldn't open the file.\n");
    
      fclose(fpIn);
    
    }
    
    
    #include "my.h"
    
    int getInput (FILE** fpIn, int** a)
    {
      int i,j;
      fscanf(*fpIn, "%d",&i);
      *a= (int*) calloc(i,sizeof(int));
      for(j=0;j<i;j++){
        fscanf(*fpIn, "%d",&(*a)[j]);
      }
      return i;
    
    }
    You might want to read our FAQ on casting malloc.

    Also, don't treat { } as optional.
    The cost of adding them (zero if you have a smart IDE) is minimal compared to the time it takes to find some bugs caused by their absence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 05:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:06 PM

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