Thread: array of pointer give error above 11

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    array of pointer give error above 11

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXWORD 30 /* max word size in characters */
    #define N 10000 /* max number of words */
    
    void sort_words(char *w[], int n);
    void swap(char **, char **);
    
    int main()
    {
        char *w[N]; /* an array of pointers */
        char word[MAXWORD];
        int n; /* number of words to be sorted */
        int i; /* loop index variable */
        
        char words;
           n=0;
           
        do {
        
        words=getchar();
        word[n]=words;
        
        w[n]=&word[n];
             printf("n is %d%c\n",n,*w[n]);
        n++;
      } while (words != EOF); 
       
       printf("%c\n",*w[11]);
       
       return 0;
    }
    
    void sort_words(char *w[], int n)
    {
        
     
    }
    
    void swap(char **p, char **q)
    {
    /* for you to complete ... */
    }
    whenever I want to display w[11] and below it give me window error but above 11 is fine . anyone have any idea ?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you actually entering in 11 characters? If you don't enter in at least 11, then it will crash.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just about all of that code is not doing what you really want to do.
    It's not very useful to have 10000 pointers that all point to a total of 30 bytes. There's only 30 different places they could point to in that array afterall...

    You'll need to learn how to use malloc and free.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by guest18 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXWORD 30 /* max word size in characters */
    #define N 10000 /* max number of words */
    
    void sort_words(char *w[], int n);
    void swap(char **, char **);
    
    int main()
    {
        char *w[N]; /* an array of pointers */
        char word[MAXWORD];
        int n; /* number of words to be sorted */
        int i; /* loop index variable */
        
        char words;
           n=0;
           
        do {
        
        words=getchar();
        word[n]=words;
        
        w[n]=&word[n];
             printf("n is %d%c\n",n,*w[n]);
        n++;
      } while (words != EOF); 
       
       printf("%c\n",*w[11]);
       
       return 0;
    }
    
    void sort_words(char *w[], int n)
    {
        
     
    }
    
    void swap(char **p, char **q)
    {
    /* for you to complete ... */
    }
    whenever I want to display w[11] and below it give me window error but above 11 is fine . anyone have any idea ?
    That code doesn't really make sense. What are you trying to do?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Quote Originally Posted by iMalc View Post
    Just about all of that code is not doing what you really want to do.
    It's not very useful to have 10000 pointers that all point to a total of 30 bytes. There's only 30 different places they could point to in that array afterall...

    You'll need to learn how to use malloc and free.
    its school assignment and requirement

    Quote Originally Posted by bithub View Post
    Are you actually entering in 11 characters? If you don't enter in at least 11, then it will crash.
    printf("%c\n",*w[11]); <- I mean this one, if I put 11 and below it crash , but if I put 12 and above it work fine

    Quote Originally Posted by Sebastiani View Post
    That code doesn't really make sense. What are you trying to do?
    the school assignment said I got to use redirection to enter a txt into the program
    and whatever is inside this txt will be stored and sorted

    I guess the reason why they got array and array of pointer is so we use the array to display and use the pointer to sort them.

    but isnt array can act as a pointer as well ? I mean the address for &array and array is different and thus array is essentially just pointing to where the data is stored.
    Last edited by guest18; 08-18-2009 at 03:54 PM.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    printf("n is %d%c\n",n,*w[n]);
    there aren't enough parameters to this call to printf

    printf("%c\n",*w[11]);
    You are attempting to dereference a pointer with *w[11], which is a seg fault.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    printf("n is %d%c\n",n,*w[n]);
    there aren't enough parameters to this call to printf
    Yes there is.

    printf("%c\n",*w[11]);
    You are attempting to dereference a pointer with *w[11], which is a seg fault.
    It is only a seg fault if less than 11 characters are typed in from the console.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guest18 View Post
    the school assignment said I got to use redirection to enter a txt into the program
    and whatever is inside this txt will be stored and sorted
    Can anyone clue me in on what "redirection" might refer to here?

    I guess the reason why they got array and array of pointer is so we use the array to display and use the pointer to sort them.

    but isnt array can act as a pointer as well ? I mean the address for &array and array is different and thus array is essentially just pointing to where the data is stored.
    A serious problem would be that with the array *w[N], none of the pointers has any memory assigned to it. What is each member of *w[N] supposed to point to? Whatever it is, you need to assign space for that.

    Think about this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    	int i;
    	char word_array[3][10];
    
    	for (i=0; i<3; i++) {
                 printf("Enter a word 9 characters or less: ");
                 scanf("%9s",word_array[i]);
            }
    	for (i=0; i<3; i++) printf("%s\n",word_array[i]);;
    		
    	return 0;
    }
    Last edited by MK27; 08-18-2009 at 08:15 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    A serious problem would be that with the array *w[N], none of the pointers has any memory assigned to it.
    Yes they do. See the line:
    >> w[n]=&word[n];
    It's a fairly convoluted algorithm, and I'm not sure what it's trying to accomplish, but the pointers do point to valid memory as long as you don't attempt to index any higher than the amount of characters entered at the console.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    Yes they do. See the line:
    >> w[n]=&word[n];
    It's a fairly convoluted algorithm, and I'm not sure what it's trying to accomplish, but the pointers do point to valid memory as long as you don't attempt to index any higher than the amount of characters entered at the console.
    Fair enough. But I bet high that "what is really going on" is that someone is typing in entire words, etc, thereby doing the loop quite a few times, and creating an index to each character, which is unlikely to be conducive to this goal:

    the school assignment said I got to use redirection to enter a txt into the program
    and whatever is inside this txt will be stored and sorted
    Perhaps (guest18 will have to answer this question) the hope was to separate the words in a sentence entered and sort them?

    I still do not understand this use of the word "redirection".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Microsoft Corporation

    this is redirection

    basically it is entering a file after exe when you executing it

    c:\assignment\assignment1question2.exe <- a.txt

    Quote Originally Posted by bithub View Post
    Yes they do. See the line:
    >> w[n]=&word[n];
    It's a fairly convoluted algorithm, and I'm not sure what it's trying to accomplish, but the pointers do point to valid memory as long as you don't attempt to index any higher than the amount of characters entered at the console.
    the whole idea of this assignment is to see how array and pointer are related to each other
    Last edited by guest18; 08-18-2009 at 07:03 PM.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guest18 View Post
    this is redirection
    Yes, I am very familiar with that use of the word ("file redirection" or "stream redirection"). That is actually not at all part of the C language, however.

    So you mean the input is to come from stdin, like this:

    myprog < thefile.txt

    In this case using getchar() or scanf(), which process stdin, are fine. Sorry for my misunderstanding.

    Can you give an example of the input text which must be sorted?
    Last edited by MK27; 08-18-2009 at 07:10 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Quote Originally Posted by MK27 View Post
    Yes, I am very familiar with that use of the word. That is actually not at all part of the C language.

    So, in what sense would you
    c:\assignment\assignment1question2.exe < a.txt

    yup it is not a part of C, I know but the assignment need me to do redirection to enter content of a txt file into the program

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guest18 View Post
    yup it is not a part of C, I know but the assignment need me to do redirection to enter content of a txt file into the program
    Hey, sorry, I was editing my post while you posted (read it again)! Anyway your use of the word "redirection" is fine, that was me being "absent minded".

    Anyway, can you give an example of the input text and how it must be sorted (alphabetically, numerically, etc)?
    Last edited by MK27; 08-18-2009 at 07:18 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    A is for apple or alphabet pie
    which all get a slice of, come taste it and try.

    this is what contained inside the txt

    for sorting part I think I just sort it abcd and such, IM pretty sure I can copy and paste one of the many sorting algorithm on the internet

    the assignment said need to sort it like this

    A
    a
    all
    alphabet
    and
    apple
    come
    for
    get
    is
    it
    of,
    or
    pie
    slice
    taste
    try.
    which

    but the getchar just get individual character each and Im not sure how to put a whole word so I just sort the individual character
    Last edited by guest18; 08-18-2009 at 08:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. Pointer with Multi-dimensional Array
    By whichet in forum C Programming
    Replies: 7
    Last Post: 11-28-2007, 12:26 AM
  5. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 06:18 PM