今天我想尝试一下我在struct中学到的东西,但是不知道它没有编译......
这是我的代码
struct Addresse
{
char street[20];
};
struct Person
{
char name[20];
int age;
char job[20];
struct Addresse mainstreet;
};
void callbyReference(struct Person* p)
{
printf("%s,%d,%s,%s\n",p->name,p->age,p->job,p->mainstreet.street );
}
int main(int argc, char const *argv[])
{
struct Person *person;
person = (struct Person*) malloc(sizeof(struct Person));
strcpy(person.name, "Max");
person.age = 14;
strcpy(person.job,"firefighter");
strcpy(person.mainstreet.street,"Fressnitz");
callbyReference(&person);
free(person)
return 0;
}
当我编译它时,会出现此错误警告:请求成员作业不是结构或联合。
答案 0 :(得分:3)
您的代码中存在两个错误。
使用Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Application Specific Information:
ProductBuildVersion: 9A235
UNCAUGHT EXCEPTION (NSInternalInconsistencyException): Could not find class named UIImage
UserInfo: (null)
Hints:
0: Sending popUp: to <IBButtonComboBoxCell: 0x7f9d15fb92a0> from <IBButtonComboBox: 0x7f9d18549aa0>
Backtrace:
0 __exceptionPreprocess (in CoreFoundation)
1 DVTFailureHintExceptionPreprocessor (in DVTFoundation)
2 objc_exception_throw (in libobjc.A.dylib)
3 +[NSException raise:format:arguments:] (in CoreFoundation)
4 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] (in Foundation)
5 -[IBBinaryUnarchiver classForClassName:] (in IBFoundation)
6 -[IBBinaryUnarchiver decodeClass] (in IBFoundation)
7 -[IBBinaryUnarchiver decodeObject] (in IBFoundation)
8 -[IBBinaryUnarchiver decodeObjectReferenceIfPossible] (in IBFoundation)
9 -[NSDictionary(IBBinaryArchivingAdditions) initWithBinaryUnarchiver:] (in IBFoundation)
10 +[NSObject(IBBinaryArchivingAdditions) decodeWithBinaryUnarchiver:] (in IBFoundation)
11 -[IBBinaryUnarchiver decodeObject] (in IBFoundation)
12 -[IBUIMetricRepresentation initWithBinaryUnarchiver:] (in IBCocoaTouchToolFoundation)
13 +[NSObject(IBBinaryArchivingAdditions) decodeWithBinaryUnarchiver:] (in IBFoundation)
14 -[IBBinaryUnarchiver decodeObject] (in IBFoundation)
15 -[IBUISimulatedTabBarControllerMetricChain initWithBinaryUnarchiver:] (in IBCocoaTouchToolFoundation)
16 +[NSObject(IBBinaryArchivingAdditions) decodeWithBinaryUnarchiver:] (in IBFoundation)
17 -[IBBinaryUnarchiver decodeObject] (in IBFoundation)
18 +[IBBinaryUnarchiver unarchiveObjectWithDataArray:context:] (in IBFoundation)
19 -[IBUISimulatedMetricChain copyWithPrecedingMetric:] (in IBCocoaTouchToolFoundation)
20 -[IBUISimulatedMetricChainVerificationState rebuildSimplifiedChain] (in IDEInterfaceBuilderCocoaTouchIntegration)
21 +[IBUISimulatedMetricChainValidator validatedMetricChain:forViewControllerWithLeafMetric:] (in IDEInterfaceBuilderCocoaTouchIntegration)
22 -[IBUIViewController(IBUIViewControllerIntegration) ibUpdateInheritedSimulatedMetrics:] (in IDEInterfaceBuilderCocoaTouchIntegration)
23 -[IBStoryboardMetricsInferrer recursivelyApplyMetricsInheritenceToController:visited:inheritanceSegues:] (in IDEInterfaceBuilderKit)
24 -[IBStoryboardMetricsInferrer recursivelyApplyMetricsInheritenceToController:visited:inheritanceSegues:] (in IDEInterfaceBuilderKit)
25 -[IBStoryboardMetricsInferrer rebuildInferredMetrics] (in IDEInterfaceBuilderKit)
26 -[DVTDelayedInvocation runBlock:] (in DVTFoundation)
27 -[DVTDelayedInvocation invokeIfNeeded] (in DVTFoundation)
28 -[DVTDelayedInvocation invoke] (in DVTFoundation)
29 -[IBDocument sceneUpdateManager:marshallableObjectPackageForRootObject:] (in IDEInterfaceBuilderKit)
30 -[IBSceneUpdateManager _updateScenes] (in IDEInterfaceBuilderKit)
31 -[IBSceneUpdateManager _updateScenesIteratively] (in IDEInterfaceBuilderKit)
32 __48-[DVTDelayedInvocation initWithTarget:selector:]_block_invoke (in DVTFoundation)
33 -[DVTDelayedInvocation runBlock:] (in DVTFoundation)
34 __NSFirePerformWithOrder (in Foundation)
35 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ (in CoreFoundation)
36 __CFRunLoopDoObservers (in CoreFoundation)
37 __CFRunLoopRun (in CoreFoundation)
38 CFRunLoopRunSpecific (in CoreFoundation)
39 RunCurrentEventLoopInMode (in HIToolbox)
40 ReceiveNextEventCommon (in HIToolbox)
41 _BlockUntilNextEventMatchingListInModeWithFilter (in HIToolbox)
42 _DPSNextEvent (in AppKit)
43 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] (in AppKit)
44 -[DVTApplication nextEventMatchingMask:untilDate:inMode:dequeue:] (in DVTKit)
45 -[NSComboBoxCell filterEvents:] (in AppKit)
46 -[NSComboBoxCell popUp:] (in AppKit)
47 _os_activity_initiate_impl (in libsystem_trace.dylib)
48 -[NSApplication(NSResponder) sendAction:to:from:] (in AppKit)
49 __37-[DVTApplication sendAction:to:from:]_block_invoke (in DVTKit)
50 DVTInvokeWithFailureHint (in DVTFoundation)
51 -[DVTApplication sendAction:to:from:] (in DVTKit)
52 -[NSControl sendAction:to:] (in AppKit)
53 __26-[NSCell _sendActionFrom:]_block_invoke (in AppKit)
54 _os_activity_initiate_impl (in libsystem_trace.dylib)
55 -[NSCell _sendActionFrom:] (in AppKit)
56 -[NSButtonCell _sendActionFrom:] (in AppKit)
57 __48-[NSCell trackMouse:inRect:ofView:untilMouseUp:]_block_invoke (in AppKit)
58 _os_activity_initiate_impl (in libsystem_trace.dylib)
59 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] (in AppKit)
60 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] (in AppKit)
61 -[NSComboBoxCell trackMouse:inRect:ofView:untilMouseUp:] (in AppKit)
62 -[NSComboBox mouseDown:] (in AppKit)
63 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] (in AppKit)
64 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] (in AppKit)
65 -[NSWindow(NSEventRouting) sendEvent:] (in AppKit)
66 -[IDEWorkspaceWindow sendEvent:] (in IDEKit)
67 -[NSApplication(NSEvent) sendEvent:] (in AppKit)
68 -[IDEApplication sendEvent:] (in IDEKit)
69 -[NSApplication run] (in AppKit)
70 NSApplicationMain (in AppKit)
71 start (in libdyld.dylib)
abort() called
Application Specific Signatures:
NSInternalInconsistencyException
分配struct Person
个实例并将其分配给malloc()
变量后,您将使用person
运算符访问其成员,但自{{1}你需要使用.
运算符的指针:
person
修复后,您将收到一个新错误,因为->
需要一个struct Person *person;
...
strcpy(person->name, "Max");
person->age = 14;
strcpy(person->job,"firefighter");
strcpy(person->mainstreet.street,"Fressnitz");
指针,即一个直接指向实际callbyReference()
实例的指针,但是您正在传递它改为使用struct Person *
指针,即指向Person
实例的指针的指针:
struct Person **
因此,在将Person
变量传递给struct Person *person;
...
callbyReference(&person);
时,请删除&
地址运算符,因为它已经是一个合适的指针:
person
如果您在堆栈上而不是callbyReference()
分配了callbyReference(person);
,那么您的代码将正确使用.
和&
运算符,例如:
person
答案 1 :(得分:0)
要访问结构人员的元素,您应该使用->
,而不是.
运算符。
正如你在第21行所做的那样; p->mainstreet.street
,这是您应该使用箭头选择运算符(->
)和点(.
)的方法。在这种情况下,点运算符用于访问另一个结构的成员,即Address
,而箭头选择运算符用于访问其外部结构的成员。
以下是可用的代码,作为对代码的修改。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Addresse
{
char street[20];
};
struct Person
{
char name[20];
int age;
char job[20];
struct Addresse mainstreet;
};
void callbyReference(struct Person* p)
{
printf("%s,%d,%s,%s\n",p->name,p->age,p->job,p->mainstreet.street );
}
int main(int argc, char const *argv[])
{
struct Person *person;
person = (struct Person*) malloc(sizeof(struct Person));
strcpy(person->name, "Max");
person->age = 14;
strcpy(person->job,"firefighter");
strcpy(person->mainstreet.street,"Fressnitz");
callbyReference(person);
free(person);
return 0;
}