Dynamically declare variables/structure in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?

No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.

Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.

Related

Format specifier to print following data in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone help me in printing the following data in C, exact format specifier and procedure to print.
union
{
char c[8];
short s[4];
long l[2];
void * vfp[2];
} info;
When i try using printf("%s\n", info.c); and printf("%s\n",info.s). I got some garbage values.
Also i need help in printing those pointers.
Thanks for the support.
You need to understand that the usage semantics of a union. You can only read the type that you stored inside a particular object instance of union. And you can only store one type in a union at a particular instance in time.
So either you stored s or c, it cannot be both. If your intention is to store both the types then what you need is a structure and not a union.
First of all you're using a union. the memory is shared between those 4 arrays. I'm not sure you want that.
Second, the list of format specifiers is here. %s is for strings. %p is for pointers, and %ld can be used for a signed long.

Does vector in ARGV mean one-dimensional array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Just curious about the term vector in programming field?
Yes, here "vector" means a one-dimensional array. This is a bit confusing because to represent a mathematical vector also uses a one-dimensional array, to store the coordinates in each of the dimensions, but this is a different usage. In this case, "vector" bring up an image of all of the elements of the array laid out in a line.
If you're referring to C, C++ or Perl (all of which use the term "argv", though Perl is the only one in which it's usually capitalized as "ARGV"): yes, it means a one-dimensional array.
The term "vector" has other uses, but they're usually pretty close to "one-dimensional array" too. For instance, the C++ standard library defines a family of vector types, which behave like extensible arrays.
(There are other uses -- for instance, "interrupt vectors" -- that have nothing to do with this. I assume you aren't interested in those.)

Get Size Of File From Commandline Input In C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to get the size of a file from the commandline in C using argv. I'm not too familiar with file i/o in C, so any pointers would be greatly appreciated. Thanks.
You've not stated the platform, but your C program is given an argument list when it is started, and the file names are strings. The POSIX function you'd probably use is stat(); it takes a pointer to a struct stat and will put the file's size into the st_size member of the structure.
The answer may be different on Windows; the POSIX subsystem will provide a stat() workalike (probably named _stat()), but there'll also be a native interface.

c code get pointer variable name within a function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
ok guys,
this is a very good question in C code (not c++):
void *pName=function();
i'd like within "function()" to get the name of the pointer "pName", of course without passing it as an argument string to the same function.
and if you are really so good in C you can provide me the type of pointer, i mean for example:
char *pName=function();
i'd like to get name and type , so "pName" and "char" within the function.
thank you :)
This is not possible. The best you could do is write a macro (shudder) that passes the type and name as arguments to the function without you having to write them a second time.

How to generate 8 byte hex value? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I want to generate this sequential data in C:
data_packet[1] = 0706050403020100 (seed_value)
next
data_packet[2] = 0f0e0d0c0b0a0908
Next will be the next 8 hexadecimal characters and so on for say 100 bytes. How can I do it? Can we do it using character array?
You don't want to use a char array since char may or may not be signed (implementation defined). If you are playing with hexadecimal numbers from 0x00 to 0xFF, I strongly recommend using an unsigned char array.
Looks like the values in the array are sequential, from 0 to N. This indicates using a for loop.
The array is a fixed size of 8 bytes. Hmmm, another good candidate for a for loop.
The hard part of your task is the direction of the bytes. For example are filling in the array starting at position 7 or at position 0?
So here is some psuedo code to help you along:
For each value from 0 to N do:
begin
for array position from 0 to 7 do: // or from 7 to 0
put 'value' into the array at 'position';
call function to process the array.
end
For more fun, change the functionality of "put 'value'" to "put random value".
If you want us to actually write your program, let us know. I could use the extra money. :-)

Resources