Thread: how to create array who point to some value?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    how to create array who point to some value?

    HI

    I have to create dynamic array, where each element of it points to some value.

    I know how to create dynamic array
    Code:
    array_record * record_1 = (array_record*)malloc( (group!/(2!(group!-2)!)) *sizeof(array_record));
    But i don't know how i create this case. for example what i want if array elements are:
    Code:
    index  value   value
    0       01  -> 345
    1       02 -> 457
    2       03 -> 689
    3       21 -> 634
    so if i have value somewhere 01 match with 01 in this array it display 345. Can you please help me how i can implement it?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by gevni View Post
    I have to create dynamic array, where each element of it points to some value.
    Dynamically create an array of pointers to something (for example, a struct type).

    Quote Originally Posted by gevni View Post
    I know how to create dynamic array
    Code:
    array_record * record_1 = (array_record*)malloc( (group!/(2!(group!-2)!)) *sizeof(array_record));
    By this example, you have demonstrated you don't know how to dynamically allocate memory. The syntax (e.g. the presence of exclamation marks) is invalid, and will not compile.

    Also, it is not necessary in C to do a type conversion on the value returned by malloc().

    Quote Originally Posted by gevni View Post
    But i don't know how i create this case. for example what i want if array elements are:
    Code:
    index  value   value
    0       01  -> 345
    1       02 -> 457
    2       03 -> 689
    3       21 -> 634
    so if i have value somewhere 01 match with 01 in this array it display 345. Can you please help me how i can implement it?
    Define a struct type to hold the required values in association with each other. Dynamically allocate an array of that struct type, and then populate it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make an array to point to another array
    By Dave11 in forum C Programming
    Replies: 4
    Last Post: 03-04-2014, 04:17 AM
  2. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  3. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM
  4. point into an array
    By ekarapanos in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 09:10 AM
  5. How to point a pointer to an array
    By ripper079 in forum C Programming
    Replies: 8
    Last Post: 08-12-2002, 07:19 PM