Thread: Trouble Accesing Array members

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Trouble Accesing Array members

    Sorry for the many questions but am about to go nuts here. This worked in another project of mine but now it just keeps crashing my program.

    Here is the troubled code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i,j,m,p, k;
        char* *setArray;
        char newsetArray[10];
        for(p=0; p<10; p++){
        newsetArray[p] = 0;
        }
        setArray = calloc(3, sizeof(char*));
        for(i=0; i<3; i++){
            setArray[i]=calloc(200, sizeof(char));
            printf("\nType your set:\n");
            gets(setArray[i]);
        }
        compare(setArray[0],setArray[1]);  <---------------Look here
    
        return 0;
    }
    
    int compare(char *c1, char *c2)
    {
    
        printf(c1[0]);  <------------------- and here
    
        return 0;
    }
    The trouble spot is in the red. Basically the function gets passed a string that's also stored in an array and then the function breaks down the string. (am about to compare the values). Now this worked before but now it keeps crashing. Now I tested it with this:

    printf(c1);

    And that confirm that the string is passed since it prints out everything. But I want to split up the string and don't understand why its causing these problems.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Try printf("%c",c1[0]); or putchar(c1[0]); instead.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bman900 View Post
    Sorry for the many questions but am about to go nuts here. This worked in another project of mine but now it just keeps crashing my program.

    The trouble spot is in the red. Basically the function gets passed a string that's also stored in an array and then the function breaks down the string. (am about to compare the values). Now this worked before but now it keeps crashing. Now I tested it with this:

    printf(c1);

    And that confirm that the string is passed since it prints out everything. But I want to split up the string and don't understand why its causing these problems.
    How about posting the actual code that causes the problem?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    How about posting the actual code that causes the problem?

    I did, its the part in red in my first post.


    Quote Originally Posted by BillyTKid View Post
    Try printf("%c",c1[0]); or putchar(c1[0]); instead.

    thanks man! That worked but now am wondering how come it worked up top without the %c. Right under the call to the compare function.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2008, 12:46 AM
  3. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM