03-09-2002
The way you are handling the name looks reasonable. You have an array of pointers to char, you calloc just enough space to hold each name, and you return the pointer. But with the name you are dealing with data whose length you cannot predict.
But the length of a 4 digit integer is predictable. And you can store all 4 digits in one int. So you should have an array of ints. You should pass that array to your function. And your function should just store the int in the array.
Your line
int *refNumber[4];
worries me. That is an array with 4 elements. Each element is a pointer to int. You still have no place to store any ints.
Do you really have a need to split the digits up, one per element? If so I would use chars rather than ints.
If i and j are integers, you can copy one to the other with just
i=j;
you don't really want to do something like:
strcpy((char *) &i, (char *) &j, sizeof int);
that's crazy.
If you need more help with this, I would need to how you allocate the data which is passed to the function, and the actual call to the function. If you change the prototype, show the new prototype as well.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
hi
I want to echo the variable $i while it auto-increments till 21
I set initially i to 1
any idea how to do that?
thank you (4 Replies)
Discussion started by: melanie_pfefer
4 Replies
2. UNIX for Dummies Questions & Answers
HI
I want to round off an integer to the next multiple of 10 in shell script.
(i.e.,) 91 should be rounded off to 100
and 90 should be rounded off to 90
It would be very helpful, if you can help me in this.
Thanks in advance (4 Replies)
Discussion started by: dayamatrix
4 Replies
3. Web Development
PHP question...
I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names.
So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc
So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies
4. UNIX for Dummies Questions & Answers
Hey I am trying to calculate a number but I found out the expr I knew works only with integers. Any help. I want to calculate (120/220) *100.
Thanks! (2 Replies)
Discussion started by: #moveon
2 Replies
5. UNIX for Dummies Questions & Answers
Hi all,
is there an easy way to convert integer to string in bash?
I have numbers like 1, 2, ..., 112, ...
and I would like to get
001 002 003 004 ...
Thank you,
Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies
6. Programming
if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies
7. Solaris
hello,
i am writing a script that takes the UID from the PASSWD and then i want
to increse the Number by one. for the Next user.
i cannot get this to work that a variable is as interger
example:
set i = 0
set $i = $+1
it's in tcsh if it's mather (10 Replies)
Discussion started by: shatztal
10 Replies
8. Shell Programming and Scripting
Hi fellows!!
i'm doing something which is not working out for me properly which i don't understand why
nowdate=`date +%s`
echo $nowdate
now the problem how to convert a date which is stored in a variable
mydate="22/Oct/2011"
mydate=`date -d '$mydate' +%s`
it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies
9. Shell Programming and Scripting
hi,
how to I do this?
i="4.000"
if ; then
echo "smaller"
fi
how do I convert the "4.000" to 4?
Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies
10. Programming
Hello and Good day, I am currently studying C and I just finished learning about variables mainly those of integer type.
I am wondering if the list below are all there is to integer variables and there are still more that i have to learn.
Here are the list:
Char
Short
int
long
long long... (3 Replies)
Discussion started by: split_func0
3 Replies
LEARN ABOUT FREEBSD
lsearch
LSEARCH(3) BSD Library Functions Manual LSEARCH(3)
NAME
lsearch, lfind -- linear search and append
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <search.h>
void *
lsearch(const void *key, void *base, size_t *nelp, size_t width, int (*compar)(const void *, const void *));
void *
lfind(const void *key, const void *base, size_t *nelp, size_t width, int (*compar)(const void *, const void *));
DESCRIPTION
The lsearch() and lfind() functions walk linearly through an array and compare each element with the one to be sought using a supplied com-
parison function.
The key argument points to an element that matches the one that is searched. The array's address in memory is denoted by the base argument.
The width of one element (i.e., the size as returned by sizeof()) is passed as the width argument. The number of valid elements contained in
the array (not the number of elements the array has space reserved for) is given in the integer pointed to by nelp. The compar argument
points to a function which compares its two arguments and returns zero if they are matching, and non-zero otherwise.
If no matching element was found in the array, lsearch() copies key into the position after the last element and increments the integer
pointed to by nelp.
RETURN VALUES
The lsearch() and lfind() functions return a pointer to the first element found. If no element was found, lsearch() returns a pointer to the
newly added element, whereas lfind() returns NULL. Both functions return NULL if an error occurs.
EXAMPLES
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
static int
element_compare(const void *p1, const void *p2)
{
int left = *(const int *)p1;
int right = *(const int *)p2;
return (left - right);
}
int
main(int argc, char **argv)
{
const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
size_t element_size = sizeof(array[0]);
size_t array_size = sizeof(array) / element_size;
int key;
void *element;
printf("Enter a number: ");
if (scanf("%d", &key) != 1) {
printf("Bad input0);
return (EXIT_FAILURE);
}
element = lfind(&key, array, &array_size, element_size,
element_compare);
if (element != NULL)
printf("Element found: %d0, *(int *)element);
else
printf("Element not found0);
return (EXIT_SUCCESS);
}
SEE ALSO
bsearch(3), hsearch(3), tsearch(3)
STANDARDS
The lsearch() and lfind() functions conform to IEEE Std 1003.1-2001 (``POSIX.1'').
HISTORY
The lsearch() and lfind() functions appeared in 4.2BSD. In FreeBSD 5.0, they reappeared conforming to IEEE Std 1003.1-2001 (``POSIX.1'').
BSD
April 21, 2013 BSD