473,769 Members | 8,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Victor <vh*******@yaho o.comwrites:
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[] = {
0xa0a0000000000 0aaLL,
[...]
0xABCDEF0123456 789LL,
};
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?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Nov 9 '08 #1
3 3043
On Nov 9, 9:21*am, Keith Thompson <ks...@mib.orgw rote:
Victor <vhnguy...@yaho o.comwrites:
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[] = {
* * 0xa0a0000000000 0aaLL,
[...]
* * 0xABCDEF0123456 789LL,
* };

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?

--
Keith Thompson (The_Other_Keit h) ks...@mib.org *<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"
Hi,

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

unsigned long long dataPatternArra y[] = {
0xa0a0000000000 0aaLL,
0xFFFFFFFFFFFFF FFFLL,
0x5555555555555 555LL,
0xAAAAAAAAAAAAA AAALL,
0xCCCCCCCCCCCCC CCCLL,
0x3333333333333 333LL,
0xEEEEEEEEEEEEE EEELL,
0x7777777777777 777LL,
0x0000000000000 001LL,
0xFFFFFFFFFFFFF FFELL,
0xABCDEF0123456 789LL,
};

Again, I can successfully access my elements via:

dataPatternArra y[0]
dataPatternArra y[1]
dataPatternArra y[2]
....
dataPatternArra y[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 *)dataPatternAr ray[i]);
}

Then the above WON'T WORK because of compile error: undefined
reference to `memcpy
However if I simply hardcode value for "i" like in the following, then
it works. Any idea?

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

Bottom line is I need to CYCLE THRU all patterns to retrieve them.
Why doesn't it work?

Thanks,
Victor
Nov 9 '08 #2
Victor <vh*******@yaho o.comwrites:
On Nov 9, 9:21*am, Keith Thompson <ks...@mib.orgw rote:
>Victor <vhnguy...@yaho o.comwrites:
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[] = {
* * 0xa0a0000000000 0aaLL,
[...]
* * 0xABCDEF0123456 789LL,
* };

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 dataPatternArra y[] = {
0xa0a0000000000 0aaLL,
0xFFFFFFFFFFFFF FFFLL,
0x5555555555555 555LL,
0xAAAAAAAAAAAAA AAALL,
0xCCCCCCCCCCCCC CCCLL,
0x3333333333333 333LL,
0xEEEEEEEEEEEEE EEELL,
0x7777777777777 777LL,
0x0000000000000 001LL,
0xFFFFFFFFFFFFF FFELL,
0xABCDEF0123456 789LL,
};
Ok.
Again, I can successfully access my elements via:

dataPatternArra y[0]
dataPatternArra y[1]
dataPatternArra y[2]
...
dataPatternArra y[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 *)dataPatternAr ray[i]);
}
You're taking dataPatternArra y[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", dataPatternArra y[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_Keit h) ks***@mib.org <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"
Nov 9 '08 #3
Victor wrote:
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 *)dataPatternAr ray[i]);
}

Then the above WON'T WORK because of compile error: undefined
reference to `memcpy
I don't believe you. And your code is random nonsense.
However if I simply hardcode value for "i" like in the following, then
it works. Any idea?

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

Bottom line is I need to CYCLE THRU all patterns to retrieve them.
Why doesn't it work?
Because you haven't bothered to learn even the most basic aspects of C.

/* mha: _Please_ post the real code. Your messages are really a mess.
* Your problem is not reproduceable, simply because you have hidden
* your error by snipping it away. Your description of your problem is
* absurd: by "diagnosing " it yourself, you have only fooled yourself.
* If the following compiles and runs, then you are completely wrong
* about what is happening. And your attempt at a printf statement
* looks like random typing. */

#include <stdio.h>

#define MAX (sizeof dataPatternArra y/sizeof *dataPatternArr ay)

int main(void)
{
unsigned long long dataPatternArra y[] = {
0xa0a0000000000 0aaLL,
0xFFFFFFFFFFFFF FFFLL,
0x5555555555555 555LL,
0xAAAAAAAAAAAAA AAALL,
0xCCCCCCCCCCCCC CCCLL,
0x3333333333333 333LL,
0xEEEEEEEEEEEEE EEELL,
0x7777777777777 777LL,
0x0000000000000 001LL,
0xFFFFFFFFFFFFF FFELL,
0xABCDEF0123456 789LL,
};
size_t i;

/* mha: note that the following has "i < MAX" instead of your "i <=
MAX". Since you have refused to tell us your definition for MAX,
we can only guess. My definition is above.

Note also that the specifier for a unsigned long long in hex is
not "%08x", and (int *)dataPatternAr ray[i] is just nonsense. If
you really wanted a pointer to print, the specifier is "%p" and
the cast is to (void *) */
for (i = 0; i < MAX; i++)
printf("Address of Pattern = %p, "
"Pattern = %#08llx\n", (void *) &dataPatternArr ay[i],
dataPatternArra y[i]);
return 0;
}

[output]
Address of Pattern = dff50, Pattern = 0xa0a0000000000 0aa
Address of Pattern = dff58, Pattern = 0xfffffffffffff fff
Address of Pattern = dff60, Pattern = 0x5555555555555 555
Address of Pattern = dff68, Pattern = 0xaaaaaaaaaaaaa aaa
Address of Pattern = dff70, Pattern = 0xccccccccccccc ccc
Address of Pattern = dff78, Pattern = 0x3333333333333 333
Address of Pattern = dff80, Pattern = 0xeeeeeeeeeeeee eee
Address of Pattern = dff88, Pattern = 0x7777777777777 777
Address of Pattern = dff90, Pattern = 0x000001
Address of Pattern = dff98, Pattern = 0xfffffffffffff ffe
Address of Pattern = dffa0, Pattern = 0xabcdef0123456 789
Nov 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
23603
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
2
3745
by: forums_mp | last post by:
I've got an STL class (see below) with two functions to store and retrieve data - msg structs. The "Store" function when called will copy the received message (depending on which message) into the appropriate array. For instance if I receive a RCVD_MSG1, I'll copy into msg1 . Upon receipt of the next RCVD_MSG1. I'll copy into msg1 . The Retrieve function when called should retrieve the latest copy. ie. msg1 or msg1 .
20
2971
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
10
6693
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
204
13092
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
5
3526
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
15
3841
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
31
7756
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
0
481
by: Pawel Dziepak | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Victor wrote: <snip> You are using an array of pointers not an array of long long values. Don't forget that sizeof(long long) != sizeof(long long*). See below for more description.
0
10197
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9977
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3549
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.