Thread: Hey, i'm new here and hoping to get a few noobish questions answered

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    ok. Cool Thanks. a few more silly questions.

    Code:
    char str[] = "This is a string to split";
            char *pword = str;
    when assigning a pointer to str why do you not use the & operator? i.e char *pword = &str;


    Code:
    splitter[wordcount].word = pword;
    i thought that with pointers in structs you had to use the -> as opposed to . or is it different if you are assiging a pointer to another pointer?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mushy View Post
    Code:
    char str[] = "This is a string to split";
            char *pword = str;
    when assigning a pointer to str why do you not use the & operator? i.e char *pword = &str;
    An array is automatically interpreted as array*, so char[] would become char* when you try to assign it somewhere. So this example is fine.
    If str was only char, then you have to use &.

    Code:
    splitter[wordcount].word = pword;
    i thought that with pointers in structs you had to use the -> as opposed to . or is it different if you are assiging a pointer to another pointer?
    x->y is a shortcut for (*x).y.
    However, the [] operator also dereferences the pointer.
    So...
    Code:
    const char* x = "My string";
    char c1 = x[0]; /* Is the same as *x */
    char c2 = x[1]; /* Is the same as *x + 1 */
    char c3 = *x[2]; /* Compile error, but it's the same as **x + 2 */
    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.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Thanks again. I'm sure you will be glad to know thats all the questions i have. ........ For now lol

    Thanks.

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Hey ok, so i got a compiler installed and im trying to run this code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAXWORDS 100
    
    typedef struct splitter{
    
          char *word;
          
    
    }s;
    
    int main(){
                      s words[MAXWORDS];
    	int wordcount = 0; 
                      int len;
                      int i;
                      char str[] = "This is some string";
                      char *ptr = str;
                      len = strlen(str);
                      
                      for(i = 0; i < len; i++)
                            {
                                if(str[i] == ' ')
                                   {
                                        words[wordcount].word = ptr;
                                        words[wordcount].word = (char *) malloc(sizeof(len));
                                        printf("%s",words[wordcount].word);
                                        str[i] =  0;
                                        ptr = &str[i +1];
                                        wordcount++;
                                   }
                            }
    return 0;
    
    }
    So i knows it assigns a pointer to each word. So how do i use the pointer to print each word to screen. It compliles but just prints jibberish to screen. I must be printing the address. But not sure how to make it work.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    words[wordcount].word = ptr;
    This will assign the pointer to the struct.

    Code:
    words[wordcount].word = (char *) malloc(sizeof(len));
    ...But this will replace the previous pointer with a pointer to newly acquired memory which you then print on your next line.

    Btw, 'tis your code?
    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.

  6. #21
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    edit My bad
    Last edited by mike_g; 01-31-2008 at 03:29 PM.

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    elysia, well its basically the same as my code with the changes matsp and you made to it earlier and then i just tried to make it print out which messed it up again.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, no, not like you want.
    First it should be
    Code:
    strcpy(words[wordcount].word, str);
    Remember: destination, source is the argument for strcpy, in order.
    Then you have to remember to truncate the string first, or you'll get the entire string copied.

    So it needs to be:
    Code:
    str[i] =  0;
    words[wordcount].word = (char *) malloc(sizeof(len));
    strcpy(words[wordcount].word, str);
    printf("&#37;s",words[wordcount].word);
    wordcount++;
    ---OR---
    Code:
    str[i] =  0;
    words[wordcount].word = str;
    printf("%s",words[wordcount].word);
    wordcount++;
    Indentation can use some work too.
    Here's an excellent guide: http://cpwiki.sf.net/User:Elyisa/Indentation
    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.

  9. #24
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    dont this i understand fully your way elysia i have this now

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAXWORDS 100
    
    typedef struct splitter{
    
          char *word;
          
    
    }s;
    
    int main(){
                      s words[MAXWORDS];
    	int wordcount = 0; 
                      int len;
                      int i;
                      char str[] = "This is some string";
                      char *ptr = str;
                      len = strlen(str);
                      
                      for(i = 0; i < len; i++)
                            {
                                if(str[i] == ' ')
                                   {
                                       str[i] =  0;
                                       strcpy(words[wordcount].word, str);
                                       words[wordcount].word = (char *) malloc(sizeof(len));
                                       strcpy(words[wordcount].word,str);
                                       printf("&#37;s",words[wordcount].word);
                                        
                                        ptr = &str[i +1];
                                        wordcount++;
                                   }
                            }
    return 0;
    
    }

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My question to you is: Do you want to copy the pointers or the data?
    Mats's example is to copy the pointers.
    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.

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    hey. I just want to split a string, using this basic setup. I know and understand that the pointer are pointing to the seperate words.

    so i was just wondering now i have a pointer to the data. How i can go about manipulating the data like a string. so i have words[0].word

    if i wanted to check using strcmp if that word was equal too "hello". How could i do that.
    basically physically break them up and give them their own space. so i can do

    printf("&#37;s", words[0].word); and it will display the first word in the string.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First you must allocate space using malloc or calloc.
    Then you can truncate the string so as to only copy a part of it.
    Then you copy the string using strcpy.
    The pointers example is more efficient, but it's also more error prone. If you would change the data somehow, it would reflect in all strings. And if the original string buffer goes out of scope, so does the other string pointers.
    The good thing about copying the data is that it won't go out of scope and won't affect the other strings. But it takes longer time to perform.
    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.

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    hey. Thanks for all you help. Got it working how i wanted. like this.

    Code:
      for(i = 0; i < len; i++)
                            {
                                if(str[i] == ' ')
                                   {
                                       str[i] =  0;
                                       words[wordcount].word = (char *) malloc(sizeof(len));
                                       strcpy(words[wordcount].word,ptr);
                                       printf("&#37;s \n",words[wordcount].word);
                                        
                                        ptr = &str[i +1];
                                        wordcount++;
                                   }
                            }
    So as far as im aware what ive done here is
    1) ran through the string replacing all spaces with 0 to signal the end of the string.
    2) i assign each pointer in the struct some space using malloc
    3)i use strcpy to tell that space to use the address of ptr which is pointing to the first word
    4) i assign pointer a new value which will be the start of the new string.

    Hope thats right

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mushy View Post
    3)i use strcpy to tell that space to use the address of ptr which is pointing to the first word
    4) i assign pointer a new value which will be the start of the new string.
    Not exactly.
    Strcpy copies a string from one buffer to another. From the original buffer to your newly allocated buffer allocated by malloc.
    Btw, you're also allocating insufficient space.
    Sizeof takes the size of a variable. In your case, len is an int, thus you allocate 4 bytes. That's not right. You should allocate the length of your string (len) + 1 (for the ending '\0').
    Also make sure to free all that space using free when you're finished with it, before the program ends.
    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.

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    I thought ptr was only an address and had no space assigned to it? How can i use strcpy to if that only takes two buffers as arguments?

Popular pages Recent additions subscribe to a feed