是否有可能printf
C中数组第一个元素的内存地址?
当我尝试:
时,编译器报告错误printf("i%/n", @array[0])
我已宣布array[] = {23, 56, 78}
。
答案 0 :(得分:5)
你有没有尝试过:
printf("%p\n", (void*)array);
答案 1 :(得分:3)
fn main() {
let filename = "tt.txt";
// open a tt.txt file in the local directory
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(filename)
.unwrap();
// now read the whole file to get the latest state
let date_re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})").unwrap();
let time_activity_re = Regex::new(r"^(\d{2}):(\d{2})\s*(.*)").unwrap();
let reader = BufReader::new(file);
let mut latest_date: Option<Date<Local>> = None;
let mut latest_datetime: Option<DateTime<Local>> = None;
let mut latest_activity: Option<String> = None;
for wrapped_line in reader.lines() {
let line = wrapped_line.unwrap();
println!("line: {}", line);
if date_re.is_match(&line) {
let captures = date_re.captures(&line).unwrap();
let year = captures.at(1).unwrap().parse::<i32>().unwrap();
let month = captures.at(2).unwrap().parse::<u32>().unwrap();
let day = captures.at(3).unwrap().parse::<u32>().unwrap();
latest_date = Some(Local.ymd(year, month, day));
latest_datetime = None;
latest_activity = None;
}
if time_activity_re.is_match(&line) && latest_date != None {
let captures = time_activity_re.captures(&line).unwrap();
let hour = captures.at(1).unwrap().parse::<u32>().unwrap();
let minute = captures.at(2).unwrap().parse::<u32>().unwrap();
let activity = captures.at(3).unwrap();
latest_datetime = Some(latest_date.unwrap().and_hms(hour, minute, 0));
latest_activity = if activity.len() > 0 {
// TODO: if latest_activity already constains a string, clear it and reuse it
// as per: https://stackoverflow.com/questions/33781625/how-to-allocate-a-string-before-you-know-how-big-it-needs-to-be
Some(activity.to_string())
} else {
None
};
println!("time activity: {} |{}|", latest_datetime.unwrap(), activity);
}
}
// FIXME: I have to open a second file descriptor to the same file, in order to be able to write to it
let mut out = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(filename)
.unwrap();
out.seek(End(0));
let now = Local::now();
if latest_date == None || latest_date.unwrap().year() != now.year()
|| latest_date.unwrap().month() != now.month()
|| latest_date.unwrap().day() != now.day()
{
if (latest_date != None) {
// not an empy file, as far as tt is concerned
out.write_all(b"\n\n");
}
out.write_all(format!("{}\n", now.format("%Y-%m-%d")).as_bytes());
out.write_all(b"\n");
}
let activity = env::args().skip(1).join(" ");
if (activity.len() > 0) {
out.write_all(format!("{} {}\n", now.format("%H:%M"), activity).as_bytes());
} else {
// if there was no latest activity *and* there is no activity, then there's no point in writing a second blank line with just a time
if latest_activity == None {
return;
}
out.write_all(format!("{}\n", now.format("%H:%M")).as_bytes());
}
// FIXME: we're just relying on the program exit to close the two file descriptors (which point at the same file).
}
应该是
printf("i%/n", @array[0])
使用格式说明符printf("%p\n", (void*)&array[0]);
打印地址
答案 2 :(得分:2)
使用%p
格式说明符。使用&
代替@
来获取地址,虽然这是多余的:&foo[0]
等于foo
,除非是sizeof
的操作数。从技术上讲,你还需要将数组指针转换为void*
,除非它是指向字符(char*
)的指针,在这种情况下不需要强制转换:
printf("%p\n", (void*)array);