Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Declare a variable without initialization

Posted on 2003-12-02
4
Medium Priority
?
313 Views
Last Modified: 2010-05-18
Hello,
after declaration of char str[1024]; (without intialization or assignment), what value the str points to? Does char str[1024]; equal to char str[1024]="";? Thanks.
0
Comment
Question by:rongz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 150 total points
ID: 9858028
Hi rongz,
> what value the str points to?
str refers to the address of first element of the array ... however, the array itself will contain junk ... it is impossible to tell what are its content. Exceptions are global and static variables which are automatically set to 0s

>Does char str[1024]; equal to char str[1024]="";?
No ... As automatic variable, it will have unpredictable contents

Cheers!
Sunny:o)
0
 

Author Comment

by:rongz
ID: 9858500
However, running the following code, I always get "hit" result. Why?

------------------------------------
#include <string.h>
#include <stdio.h>
void main(int argc, char **argv){
  char max[124];

  if (strcmp(max, "")==0)
    printf("hit\n");
  else
    printf("not hit\n");

}
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9858559
Memory has a lot of zeros in it. You got lucky.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9858587
This typically works too, but you shouldn't depend on it:
--------8<--------
#include <stdio.h>

void foo();
void foo2();

int main()
{
      foo();
      foo2();
}

void foo()
{
char buf[256];

      strcpy(buf,"Hello, World\n");
}

void foo2()
{
char buf[256];

      printf(buf);
}
--------8<--------
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question