The sizeof of "C language Learning Notes"

Source: Internet
Author: User

Code 1:

#include <stdio.h>

int main () {
	char *str = "Geeksquiz";
	Char str1[] = "Geeksquiz";
	Char str2[] = {' G ', ' e ', ' e ', ' K ', ' s ', ' Q ', ' u ', ' i ', ' Z '};
	int n = sizeof (str)/sizeof (str[0]); 4, STR is a pointer
	int n1 = sizeof (STR1)/sizeof (str1[0));//10, str1 is an array, note the ' i '
	int n2 = sizeof (STR2)/sizeof (str2[ 0]); 9, Array,

	printf ("n =%d, N1 =%d, N2 =%d", N, N1, N2);

	return 0;
}

Notes:

in Main, the name of the ' array is ' is ' the size in bytes of the ' the ' array with sizeof.

However, an array decays to a pointer when passed to a function, so you get sizeof (int*) inside the function.

Code 2:

#include <stdio.h>
/* Array name is treated as a pointer when the function argument is passed
/void func1 (int a[]) {
	printf ("%d\n", sizeof (a));//4
}

void Func2 (int *a) {
	printf ("%d\n", sizeof (a));
}
int main () {
	int array[] = {1, 2, 3};
	printf ("%d\n", sizeof (array));
	func1 (array);//4
	Func2 (array);//4 return
	0;
}

Reference: Http://stackoverflow.com/questions/9509829/c-size-of-array



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.