copy strings in array

This is a discussion on copy strings in array within the C Programming forums, part of the General Programming Boards category; hi, i have a string printed to by sprintf. i would like to: for every string printed, it copies the ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    61

    copy strings in array

    hi,

    i have a string printed to by sprintf. i would like to: for every string printed, it copies the contents into the next element of a char array:

    something like this (but this dosent work...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void addchars(char *sa[]){
    
    int i;
    char buf[100];
    
     for(i = 0; i  <= 3; ++i){
      sprintf(buf, "david %d\n",i);
      printf("%s", buf);
      strcpy(&sa[i],buf);
     }
    }
    
    
    
    int main() {
    
    char *sa[25];
    
    
    printf ("%s\n",sa[0]);
    addchars(sa);
    //printf("%s,%s,%s,%s,%s\n",sa[0],sa[1],sa[2],sa[3],sa[4]);
    printf("%s",sa[0]);
    
    return 0;
    
    
    }
    thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    As you have been told, sa is an array of pointers, which are uninitialized. Pointers are not a storage unit.
    And trying to print the contents from an uninitialized pointer will undoubtedly cause a crash right away.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    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. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    First off, you have to decide what this:
    for every string printed, it copies the contents into the next element of a char array:
    means. By definition, you cannot get a string into the next element of a char array -- the only thing you can get into the next element of a char array is a char.

    Now, since you don't have any char arrays in your code, but do have char * arrays, I assume you mean that. In that case, sa[0], sa[1], ... are already your char *s that you would want to copy to, so adding & is fruitless. HOWEVER: currently sa[0], sa[1], etc. point to somewhere in New Jersey. You need to make them point to a string inside your computer, usually using malloc.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry :s

    so: i have to make char * sa point to a space resevered by malloc?

    -> is it even possible to get sa[0] = david 1, sa[1] = david 2 etc?

    thanks for the quick response...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Yes, or just make them allocated on the stack.
    There are several ways to assign values to arrays, depending on what you need.
    If you need string literals (non-modifiable), assign a string literal to a const char* pointer.
    If you need a modifiable buffer, then create a buffer and use strcpy.
    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. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    so like malloc(sizeof(sa)); ?

    im not gonna lie anymore - i suck at string manipulation... i dont know how i would do that...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by davo666 View Post
    sorry :s

    so: i have to make char * sa point to a space resevered by malloc?

    -> is it even possible to get sa[0] = david 1, sa[1] = david 2 etc?

    thanks for the quick response...
    You need to make each element of sa point to space reserved by malloc (not the array itself, but each string in the array).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    If you would kindly state exactly what you want.
    Do you want to, for example, copy the contents of element 0 into 1, etc, until the end of the array?
    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. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    no, i want to make a string with sprintf , then put that string into the first element of an array passed from the main function, then on the second iteration the second element.

    thanks

    @tabstop: ooooh so i initialise sa[i] = malloc(sizeofmystringfromsprintf); then make that space equal buf?

    im going to try that

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Code:
    char sa[SUFFICIENT_SPACE_HERE][25];
    Boom! Your program should work (well almost), except for the printf in the beginning which tries to print an unexisting string.
    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. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    umm:

    Code:
    include <stdio.h>
    #include <string.h>
    
    void addchars(char sa[]){
    
    int i;
    char buf[100];
    
     for(i = 0; i  <= 3; ++i){
      sprintf(buf, "david %d\n",i);
      printf("%s", buf);
      strcpy(sa[i],buf);
     }
    }
    
    
    
    int main() {
    
    char sa[128][25];
    
    
    
    addchars(sa);
    printf("%s",sa[0]);
    
    return 0;
    
    
    }
    this revised code dosent work...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, what the heck happened to "char *sa[]"? Edit: Even better yet, why not make it char sa[128][25]?

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    sorry, i used an old version of addchars, and copied the new main...

    Code:
    void addchars(char *sa[]){
    
    int i;
    char buf[100];
    
     for(i = 0; i  <= 3; ++i){
      sprintf(buf, "david %d\n",i);
      printf("%s", buf);
      strcpy(sa[i],buf);
     }
    }
    
    
    
    int main() {
    
    char sa[128][25];
    
    
    
    addchars(sa);
    printf("%s",sa[0]);
    
    return 0;
    
    
    }
    still doesn't work: test.c: In function 'main':
    test.c:24: warning: passing argument 1 of 'addchars' from incompatible pointer type

    thanks for all your help though

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You'll need at least the 25 in the function declaration.

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    i still get the error msg though...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  3. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21