Thread: Is C playing tricks with me? Random Seg faults

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Is C playing tricks with me? Random Seg faults

    Header #1 city.h

    Code:
    typedefstructCity{
    
        double* lat;
        double* lon;
        double* pop;
    
    
        char* airport;
        char* name;
    
    
    }City; 
    


    Header #2 vector.h
    Code:
    Header file #2 "vector.h"
    
    typedefstructVector{
    
    
      City* cityArray[26];// 26 pointers to struct City
    
    
     }Vector;
    


    C file
    Code:
    #include"vector.h"
    #include"city.h"
    #include<stdlib.h>
    
    int main() {
    Vector *cities;
    
    init(cities); //calls function init
    
    
    void init(Vector*ptr){
    
    ptr->cityArray[0]->name ="hi";// Error Seg Fault! ptr = (Vector*) NULL
    
    }
    


    Earlier, I was able to compile and print out "hi" no problems. Now
    all of a sudden, I'm getting seg faults each time. I'm 100% sure I'm running
    the same code.

    Whats going on? Is it because in city.h char* name is not initialized? I can't
    initialize it, the compiler gives me an error when i try.
    Last edited by tmac619619; 09-14-2015 at 11:48 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that you expect to compile but which demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Tips and Tricks
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2009, 11:30 PM
  2. Seg Faults at Random
    By mrpickle in forum C Programming
    Replies: 5
    Last Post: 01-15-2004, 03:42 PM
  3. pls post here tricks that you know of in C/C++, thanks
    By mickey in forum C++ Programming
    Replies: 55
    Last Post: 06-12-2003, 04:28 PM
  4. Tricks On Sybblings
    By helbovine in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2002, 07:12 PM