Array, pointer, malloc

This is a discussion on Array, pointer, malloc within the C Programming forums, part of the General Programming Boards category; I whant to have a array like this: Msg[0] = 436; Msg[1] = 4; The values are stored in another ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Array, pointer, malloc

    I whant to have a array like this:

    Msg[0] = 436;
    Msg[1] = 4;

    The values are stored in another array. My code are like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    int DP[2];
    //DP = malloc(2*sizeof(int));
    char *Msg;
    Msg = malloc(2*sizeof(int));
    
    DP[0] = 436;
    DP[1] = 3;
    
    sprintf(&Msg[0],"%d",DP[0]);
    sprintf(&Msg[1],"%d",DP[1]);
    
    printf("%d\n",Msg[0]);
    printf("%d\n",Msg[1]);
    }
    It output 51 and 53...

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. int main()

    2.
    Code:
    int DP[2] = {436,3};
    3. sprintf expects as a first parameter char* - pointer to the buffer where the string will be stored

    If you want to create the string like "436 3" - you should allocate enough space (sizeof int has nothing to do with the number of bytes required to represent the int as a string)

    And you will use sprintf as
    sprintf(Msg,"&#37;d %d",DP[0], DP[1]);

    4. to print the char buffer you will use

    printf("%s\n",Msg);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You should also free the memory allocated by malloc() before quitting the program:
    Code:
    free(Msg);
    And could also add
    Code:
    return 0;
    to the end of your main function.


    Oh, and you say
    I whant to have a array like this:

    Msg[0] = 436;
    Msg[1] = 4;
    well... since you declare Msg as a char array, you won't be able to have such thing since a char can hold only one character.
    One way to solve this is using a 2D array, like

    Code:
    ...
    char *Msg[2];
    msg[0] = (char *) malloc(...);
    msg[1] = (char *) malloc(...);
    
    sprintf(msg[0], "%d", 436);
    sprintf(msg[1], "%d", 4);
    ...
    but it's an "overcomplex" solution for this kind of problem, knowing you could do someting like

    Code:
    char Msg[][15] = {"436", "4"};

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    And could also add
    Code:

    return 0;
    At the moment main could not even return a value as its declared void. Also I'm pretty sure that Int main() returns 0 by default.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Also I'm pretty sure that Int main() returns 0 by default.
    It does in C99 and C++, but not in C90 (I think that in C90, without the explicit return, the return value is undefined). So in C at least, to be compatible both with the more commonly used C90 and the newer C99, it's a good idea to use an explicit return.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Where does C99 state that?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Where does C99 state that?
    I don't know offhand, but it's in the FAQ:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    which BTW is off slightly since it claims that in C89/90 the explicit return is required - it's not actually required for C90 compliance, just to have a well-defined return value (which of course is a good enough reason to include it).

    Edit:

    http://c0x.coding-guidelines.com/5.1.2.2.3.html
    Last edited by robatino; 07-17-2007 at 05:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  4. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 09:29 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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