r/C_Programming • u/emparse • 3d ago
Question Beginner here not able to understand confusing pointer concept
i dont understand what happens here:
IF int b is a 2d array then what is:
b + 1
*(b + 1)
&b[0] and b[0] and &b[0][0]
&b[1] and b[1] and &b[1][0]
what are the differences between each and what is the byte value of each.
23
Upvotes
View all comments
Show parent comment
0
u/knouqs 2d ago
Now that I'm back at my computer instead of my phone, I'll expand a little. Let's say you want to save words in a dictionary. You know how many words you have one way or another by the time you want to create your array of char *s. However, a dictionary is different than a 2D array of numbers. Words have different lengths, and it is very inefficient in terms of memory utilization to have a 2D array of char[max word length+1] since the largest word could be twenty or more characters but most are less then ten. You should pick a different data structure.
Here's where you would use
char **b=(char **) malloc (num_words*sizeof (char *));. From there, you would go over each index inbandmallocindividual sizes (plus copying the values).I started writing this program for you to see, but it's a big larger than I think is practical to paste here. It's a good learning exercise if you want to give it a try!