Thread: Help needed

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Help needed

    I am writing a program that will call the unix execv program based on some of the command line arguments passed to the program, so basically I need to make an argv[] for the execv call out of the argv[] that is passed to the program. Here is how I'm doing this

    Code:
    char **programArguments;
    
      for(j = 0, i = 4; i < argc; i++, j++)
        {
          printf("Entered\n");
          programArguments[j]=  argv[i];
          printf("Exited\n");
        }
    When I compile this with both gcc and borland on my windows platform, this section of code works perfectly fine, however, when compiling using gcc on my schools unix computer, this program crashes after the first "entered" is printed with a segmentation fault. Does anyone know what the problem is here and how to fix it?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problem is that programArguments doesn't point to memory that you own. Repeat after me: Arrays and pointers are not the same.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    try this
    Code:
    char *programArguments[5]; 
    
      for(j = 0, i = 4; i < argc; i++, j++)
        {
          printf("Entered\n");
          programArguments[j]=  argv[i];
          printf("Exited\n");
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 07:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM