Character Arrays

This is a discussion on Character Arrays within the C Programming forums, part of the General Programming Boards category; Hello, I am experiencing trouble adding data to character arrays: Code: int main() { char d[32]; int i, i1; for(i ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Character Arrays

    Hello,
    I am experiencing trouble adding data to character arrays:
    Code:
    int main()
    {
    char d[32];
    int i, i1;
    for(i = 0;i<5;i++){
    for(i1 = 50;i1<82;i1++){
    sprintf(d[i]+strlen(d[i]), "%c", (char)i1);
    }
    return 0;
    }
    The Program crashes when I execute it...
    Please Help,
    Thank you very much,
    Daniel

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What is it supposed to do?

    There's no need to cast a char to a char... and strlen() takes a "string" not a char.

    Code:
    for(i1 = 50;i1<82;i1++){
    Why are you going from 50?
    Last edited by zacs7; 05-20-2007 at 04:47 AM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, you need to learn how to indent properly do you can see what is going on.

    After that, you're missing a matching '}', and you're using wrong types for functions, so I don't even know how this compiled let alone ran and crashed.

    The important line is this:

    Code:
    sprintf(d[i]+strlen(d[i]), "&#37;c", (char)i1);
    What you are doing is totally wrong. I don't think you really know what sprintf() does. I would suggesting reading up on char arrays (ie. C strings), and read the man pages (or google) for information on sprintf().

    I could go into detail about why you're crashing, but it's pretty ridiculous. d[i]+strlen(d[i]) is not a char *, let alone a char * to point to a buffer properly. sprintf() is a function to write a formatted string to a buffer. If you're just trying to add a char inside of it, then just manually do it, with something like this:

    Code:
    d[i] = (char)i1;

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Reason for sprintf

    I need to point to the end of the string.
    So as to add data, at the end

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    if you assign something to index i - you KNOW that this is the end of string... you do not need sprintf for this knowledge
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    yes yes, but what ARE you tring to do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 04:32 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 06:54 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 03:24 AM

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