Arrays in C

This is a discussion on Arrays in C within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to pass some values in an array into a single variable in C. For example: a[0] = ...

  1. #1
    ube
    ube is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Arrays in C

    Hi I'm trying to pass some values in an array into a single variable in C.

    For example:
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;

    And I want to combine it to make 123. Can anyone help me with teh syntax?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Looks like you could use a loop and a multiplier that grows 10 times larger every iteration. Where's your attempt?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    1
    Do you mean each line of the array in the order? I assume you do, it'd be using strcat.
    Something like:

    Code:
    /*First you need to declare a variable for the string:*/
    char    string;
    
    /*Now go to the code*/
    
    strcat(string, a[0])
    strcat(string, a[1])
    strcat(string, a[2])
    This would make string equal to 123.
    Or something like that anyway.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The second argument to strcat isn't a single character, so that wouldn't work anyway. Also, it looks like his array is integers, so you wouldn't use strcat there.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ube View Post
    Hi I'm trying to pass some values in an array into a single variable in C.

    For example:
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;

    And I want to combine it to make 123. Can anyone help me with teh syntax?
    Code:
    b = (a[0] * 100) + (a[1] * 10) + a[2];
    Easy as falling off a slimy log.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Code:
    int sum = 0;
    for(i = 0; i<arrLen; i++)
    {
        sum+=sum*10+_array[i];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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