Re: How to retrieve data from array of pointers or from a struct?



Victor <vhnguyenn@xxxxxxxxx> writes:
On Nov 9, 9:21 am, Keith Thompson <ks...@xxxxxxx> wrote:
Victor <vhnguy...@xxxxxxxxx> writes:
I have 2 problems:

First, I want to grep one pattern at a time from the pattern group
below:

unsigned long long i;

unsigned long long *dataPatternPtr[] = {
    0xa0a00000000000aaLL,
[...]
    0xABCDEF0123456789LL,
  };

You're declaring an array of pointers to unsigned long long, but
you're initializing the pointers with integer values.  This is
actually a constraint violation, and your compiler should have warned
you about it.  Your problem is either that you're invoking your
compiler in some non-standard mode that inhibits the warnings, or
getting warnings and not bothering to tell us about them.

What are you actually trying to do?  Do you want an array of integers
or an array of pointers?

Please don't quote signatures.

I modified my script to use an array of longs instead of array of
pointers to longs:

It's a program, not a script.

unsigned long long dataPatternArray[] = {
0xa0a00000000000aaLL,
0xFFFFFFFFFFFFFFFFLL,
0x5555555555555555LL,
0xAAAAAAAAAAAAAAAALL,
0xCCCCCCCCCCCCCCCCLL,
0x3333333333333333LL,
0xEEEEEEEEEEEEEEEELL,
0x7777777777777777LL,
0x0000000000000001LL,
0xFFFFFFFFFFFFFFFELL,
0xABCDEF0123456789LL,
};

Ok.

Again, I can successfully access my elements via:

dataPatternArray[0]
dataPatternArray[1]
dataPatternArray[2]
...
dataPatternArray[7]

and so on.

However if I use a variable "i" in a "for" loop to access each of
them:

for (i=0; i <= MAX; i++) {
printf("Pattern = %08x\n", (int *)dataPatternArray[i]);
}

You're taking dataPatternArray[i], a value of type unsigned long long,
and converting it to int* (pointer to int). This makes no sense.
You're then printing this pointer value using a "%08x" format; "%x" or
"%08x" is for printing a value of type unsigned int, not int*. (And
you're specifying at least 8 digits, but your values are 16 digits.)

Previously, you showed a declaration of i as an unsigned long long.
This isn't wrong, but since i is an index into the array, which has
only a few elements, it would make more sense to declare it as an int.

You're looping from 0 to MAX. What is MAX? If it's the number of
elements in the array, then you're going past the end of the array.
(That's not related to the symptoms you're describing.)

The way to print a value of type unsigned long long in hexadecimal is:

for (i = 0; i < MAX; i++) {
printf("Pattern = %016llx\n", dataPatternArray[i]);
}

Then the above WON'T WORK because of compile error: undefined
reference to `memcpy

You're not explicitly calling memcpy, but it's possible that your
compiler is implicitly calling memcpy to copy the value, perhaps
because the CPU doesn't have a native instruction to copy a 64-bit
value. memcpy is part of the standard library, and most
implementations will link it automatically, so you don't have to do
anything special to make memcpy available -- but apparently either
your implementation doesn't do that, or you're invoking it
incorrectly, or it's configured incorrectly. I don't think you've
told us what compiler you're using, but you should be able to find a
forum that discusses it. Write a complete self-contained program that
illustrates your problem (not the code fragments you've been posting
here) and post it either here or in a support form for your compiler.
We need something we can compile and run ourselves; if you don't know
what the problem is, you don't know what you can safely leave out. We
can help you with any C language issues, but we can't help you with
any problems you're having with your compiler.

[...]

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: code optimiation
    ... Given that the compiler can often optimise the generated code to use the best sized types available, it's seldom worth specifying "fast" types explicitly. ... pointers and floating point types whose "zero value" might not be all- ... instruction, so the assembler produced for *p++ when used as the ... It will do the same job, and let you write the source code using proper array constructs. ...
    (comp.arch.embedded)
  • Re: Q: Checking the size of a non-allocated array?
    ... an actual argument is already invalid ... First note that you don't have an unallocated array in the subroutine. ... it is comparable to disassociated or undefined pointers. ... Obviously the compiler has ...
    (comp.lang.fortran)
  • Re: How to retrieve data from array of pointers or from a struct?
    ... This defines an array of pointers to unsigned long longs... ... of your compiler, I'm sure it would have told you. ... examples/zzzmain.c:295: undefined reference to `memcpy' ... #include only adds the necessary declaration, you still need to link the ...
    (comp.lang.c)
  • Re: Strange programming problem
    ... Depending on how your array is allocated, ... >the compiler can do range checking. ... of memory without limit using pointers, so you can screw up any part ... Saying that a good and careful programmer ...
    (comp.os.vms)
  • Re: Struct assignment
    ... A structure assignment will cause a call to memcpy to be made, or equivalent code emitted if the structure is small enough to make this wasteful and the compiler is clever. ... However if structures contain pointers then the values of the pointers are overwritten. ...
    (comp.lang.c)