-
Memory Address
Hello,
I have a simple question. I've been writing a function that shows me the current address of a variables memory. So far there are multiple addresses when pointing to each of the pointers.
My code is below, and was wondering if this is the correct route to finding the address of the variable:
Code:
void memoryAddress(const char *format, ...) {
int cmd, **arg = (int **)&format + 1;
while (*format) {
if (format[0] != '%') {
format++;
continue;
}
cmd = format[1];
format += 2;
switch( cmd ) {
case 'i':
case 'd':
case 'u':
case 'c':
case 'f':
case 'g':
case 's':
printf("0x%p\n", (void *)arg); // address of data
break;
}
arg++;
}
}
int main() {
float myFloat = 0.0f;
char str[] = {"Hello"};
memoryAddress("%s %g", str, &myFloat);
return 0;
}
I do know if you get control of **arg you can write information to it. Like [*(float *)*arg] would allow you to write a floating point to your memory address.
I just want to confirm this is all correct. Though if I display [*arg] or [arg] I get other address results. Does anyone know the difference between the three results? [**arg] — [*arg] — [arg]
Edit: No this is not homework, im just curious about the different memory address results
Thank you for your time,
- Stack Overflow
-
Why the printf style format string? If all you want to do is print the addresses of each argument then really all you need to do is use, say, an integer denoting the number of arguments as the first and then inside the function, have va_arg return a pointer to void:
Code:
#include <stdarg.h>
#include <stdio.h>
void
memoryAddress(
int n,
...
)
{
va_list args;
va_start(args, n);
while (n--) {
printf("0x%p\n", va_arg(args, void *));
}
va_end(args);
}
int
main()
{
float f = 0.0f;
char s[] = "Hello";
memoryAddress(2, s, &f);
return 0;
}
-
That makes sense,
Thanks alot :) your way is much simpler and well written.
I'll have to keep the va args in mind. I guess I just got stuck with writing a similar function [sscanf()] then with the same mind set while writing this function.
- Stack Overflow
-
^^ I see how you code works but, why such an aquard indention style prelude? Nice code though
-
>why such an aquard indention style prelude?
I assume you mean the function headers since K&R formatting with a two space indent is far from awkward. :D I'll break it down for you so that you can see my reasoning behind it:
Code:
/* 1 */ void
/* 2 */ memoryAddress(
/* 3 */ int n,
/* 4 */ ...
/* 5 */ )
/* 6 */ {
The return type is on its own line (1) so that the function name begins at column 1 on the next line (2). This is so that I can avoid complicated or tedious searches for the function definition. This is as opposed to calls to the function that would always have at least a two space indention (thus not be at column 1) and the function declaration which is more conventional:
Code:
void memoryAddress(int n, ...);
Still on (2), I maintain the K&R style by beginning the argument list on the same line as the function name. There's no big reason for this other than I prefer it over:
Code:
function_name
(
args
)
Each argument (3) and (4) has its own line so that I can more easily comment them in a way that looks nice. This setup also avoids really long argument lists and line wrapping. A standardized short method is much better IMO than an ad hoc line break. Arguments are indented two spaces for readability purposes. The closing paren of the argument list is also on its own line for the same reason as the opening paren being on the same line as the function name. I prefer the look over this:
Code:
function_name(
args)
The opening brace of the function body (6) is always on column 1 just as the closing brace is for ease of navigation in vi (which I use occasionally ;)).
You may be wondering why the new style, but I'll shock everyone and say that this was always my style, I just never used it here. I won't go into detail for personal reasons, no offense. ;)
-
Okay. I use the { on the same line as a function like you well here is mine
Code:
int func(argc){
stuff indented by a tab on vim and 8 spaces here
} /*on its own line*/