Thread: Segmentation fault (pointer to an array)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    Segmentation fault (pointer to an array)

    Hey,

    Having some issues with a pointer to an array.

    Code:
    GEOMOBJ **geometricObjects;
    numOfObjects = readTextFile(fileName, geometricObjects);
    I have this function:
    Code:
    int readTextFile(char *filename, GEOMOBJ **p){
         *p = malloc(sizeof(GEOMOBJ) * numOfObjects);
         for (i = 0; i < numOfObjects; i++){
              *p[i] = i;
         }
    
         return numOfObjects;
    }
    I've determined that the segmentation fault is coming from the for loop. What I'm trying to accomplish is pass a pointer to an array to the function and have the function fill the array and return an integer.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Do you mean to do something like this?
    Code:
    GEOMOBJ *geometricObjects;
    numOfObjects = readTextFile(fileName, &geometricObjects);
    And this in the loop?
    Code:
              (*p)[i] = i;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Lang View Post
    Hey,

    Having some issues with a pointer to an array.

    Code:
    GEOMOBJ **geometricObjects;
    numOfObjects = readTextFile(fileName, geometricObjects);
    I have this function:
    Code:
    int readTextFile(char *filename, GEOMOBJ **p){
         *p = malloc(sizeof(GEOMOBJ) * numOfObjects);
         for (i = 0; i < numOfObjects; i++){
              *p[i] = i;
         }
    
         return numOfObjects;
    }
    I've determined that the segmentation fault is coming from the for loop. What I'm trying to accomplish is pass a pointer to an array to the function and have the function fill the array and return an integer.
    I'm wondering - why are returning "numOfObjects" as if you're trying to keep track of how many were read, when you already know how many objects there are considering it must be globally defined, since you use the numOfObjects variable inside the function without it being defined inside its scope.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Yes that's done exactly what I need.

    Thanks muchly.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Beware of compiler warnings!
    Passing a ** to a function with & will result in ***, which is not the ** the function wanted.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  4. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM