Re: Help in c pointers



On 2006-02-28, Pedro Graca <hexkid@xxxxxxxxxxx> wrote:
Jordan Abel wrote:
On 2006-02-28, Kenneth Brody <kenbrody@xxxxxxxxxxx> wrote:
"manochavishal@xxxxxxxxx" wrote:
int *p;
*p = 4;

char * msg;
*msg = 'c';

printf("msg is %s",msg);

Neither pointer has been initialized, and both assignments invoke UB.

Incidentally, there's a third instance of undefined behavior here -
anyone know what it is?

a) `msg' isn't (*) a c-string (a sequence of chars ending in NUL) so
passing it to printf invokes UB.

Not quite - msg isn't a valid pointer at all, so the act itself of
passing it to printf invokes UB. It would equally invoke UB if it were a
void * and being passed to a printf %p format.

The value of msg is indeterminate since it is never initialized. Reading
an indeterminate value results in undefined behavior [since it may be a
trap representation]
.



Relevant Pages

  • Re: Help in c pointers
    ... char * msg; ... Neither pointer has been initialized, and both assignments invoke UB. ... `msg' isn't a c-string so ...
    (comp.lang.c)
  • Re: Help in c pointers
    ... points to it says seg fault but for the char pointer it works fine. ... Neither p nor msg points anywhere. ... them results in undefined behavior. ...
    (comp.lang.c)
  • Re: Multithread writes to a Rich Text Box
    ... I would address the issue of multithreading first. ... Also, if msg is null, or blank, then you should look at the call stack ... My first question involves why the code is written using Invoke rather ... The final issue is the jitter that accompanies the ScrollToCaret() method ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using the Exchange Backup API
    ... possible error could be in the way I invoke it or any mismatch in parameters. ... I had checked whether GetProcAddress returns a valid pointer, ... As I said in my earlier answers I am trying to call a standard API ...
    (microsoft.public.vc.mfc)
  • Re: Calling functions of derived types, pointers and memory leakage
    ... You don't need to make a temporary variable, invoke the ... And if you have a function that returns a pointer, ... use the data from the calling routine inside of the function, ... It is important to understand something about functions and assignment ...
    (comp.lang.fortran)